Function doAwaitResponse

Helper to coallesce the promise resolved / reject into a single callback to simplify error handling.

The value or promise like value to wait to be resolved or rejected.

The callback function to call with the resulting value, if the value is not a promise like value then the callback is called synchronously, if the value is a promise then the callback will be called once the promise completes the resulting value will be passed as an IAwaitResponse instance, it will be called whether any promise resolves or rejects.

The value returned by the cb callback function, if the value is a promise then the return value of the callback will be returned as a promise whether the callback returns a promise or not.

let promise = createPromise<number>((resolve, reject) => {
resolve(42);
});

// Handle via doAwaitResponse
doAwaitResponse(promise, (value) => {
if (!value.rejected) {
// Do something with the value
} else {
// Do something with the reason
}
});

// It can also handle the raw value, so you could process the result of either a
// synchrounous return of the value or a Promise
doAwaitResponse(42, (value) => {
if (!value.rejected) {
// Do something with the value
} else {
// This will never be true as the value is not a promise
}
});
  • Helper to coallesce the promise resolved / reject into a single callback to simplify error handling.

    Type Parameters

    • T
    • TResult1 = T
    • TResult2 = never

    Parameters

    • value: T | Promise<T>

      The value or promise like value to wait

    • cb: ((response: AwaitResponse<T | TResult1, any>) =>
          | T
          | TResult1
          | TResult2
          | Promise<T | TResult1 | TResult2>)

      The callback function to call with the resulting value, if the value is not a promise like value then the callback is called synchronously, if the value is a promise then the callback will be called once the promise completes the resulting value will be passed as an IAwaitResponse instance, it will be called whether any promise resolves or rejects.

    Returns
        | T
        | TResult1
        | TResult2
        | Promise<T | TResult1 | TResult2>

    The value returned by the cb callback function, if the value is a promise then the return value of the callback will be returned as a promise whether the callback returns a promise or not.

    let promise = createPromise<number>((resolve, reject) => {
    resolve(42);
    });

    // Handle via doAwaitResponse
    doAwaitResponse(promise, (value) => {
    if (!value.rejected) {
    // Do something with the value
    } else {
    // Do something with the reason
    }
    });

    // It can also handle the raw value, so you could process the result of either a
    // synchrounous return of the value or a Promise
    doAwaitResponse(42, (value) => {
    if (!value.rejected) {
    // Do something with the value
    } else {
    // This will never be true as the value is not a promise
    }
    });
  • Helper to coallesce the promise resolved / reject into a single callback to simplify error handling.

    Type Parameters

    • T
    • TResult1 = T
    • TResult2 = never

    Parameters

    • value: T | PromiseLike<T>

      The value or promise like value to wait for

    • cb: ((response: AwaitResponse<T | TResult1, any>) =>
          | T
          | TResult1
          | TResult2
          | PromiseLike<T | TResult1 | TResult2>)

      The callback function to call with the resulting value, if the value is not a promise like value then the callback is called synchronously, if the value is a promise then the callback will be called once the promise completes the resulting value will be passed as an IAwaitResponse instance, it will be called whether any promise resolves or rejects.

    Returns
        | T
        | TResult1
        | TResult2
        | PromiseLike<T | TResult1 | TResult2>

    The value returned by the cb callback function, if the value is a promise then the return value of the callback will be returned as a promise whether the callback returns a promise or not.

    let promise = createPromise<number>((resolve, reject) => {
    resolve(42);
    });

    // Handle via doAwaitResponse
    doAwaitResponse(promise, (value) => {
    if (!value.rejected) {
    // Do something with the value
    } else {
    // Do something with the reason
    }
    });

    // It can also handle the raw value, so you could process the result of either a
    // synchrounous return of the value or a Promise
    doAwaitResponse(42, (value) => {
    if (!value.rejected) {
    // Do something with the value
    } else {
    // This will never be true as the value is not a promise
    }
    });