Helper to coallesce the promise resolved / reject into a single callback to simplify error handling.
The value or promise like value to wait
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.
The value or promise like value to wait for
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.
Param: value
The value or promise like value to wait to be resolved or rejected.
Param: cb
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
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.Example