Creates a new Promise.
Optional
stateReturns a string representation of the current state of the promise. The promise can be in one of four states.
let doResolve;
let promise: IPromise<any> = createSyncPromise((resolve) => {
doResolve = resolve;
});
let state: string = promise.state();
console.log("State: " + state); // State: pending
doResolve(true); // Promise will resolve synchronously as it's a synchronous promise
console.log("State: " + state); // State: resolved
Attaches a callback for only the rejection of the Promise.
A Promise for the completion of the callback.
Attaches a callback for only the rejection of the Promise.
A Promise for the completion of the callback.
Attaches a callback for only the rejection of the Promise.
A Promise for the completion of the callback.
Attaches a callback that is invoked when the Promise is settled (fulfilled or rejected). The resolved value cannot be modified from the callback.
Optional
onfinally: (() => void)The callback to execute when the Promise is settled (fulfilled or rejected).
A Promise for the completion of the callback.
function doFunction() {
return createPromise((resolve, reject) => {
if (Math.random() > 0.5) {
resolve('Function has completed');
} else {
reject(new Error('Function failed to process'));
}
});
}
doFunction().then((data) => {
console.log(data);
}).catch((err) => {
console.error(err);
}).finally(() => {
console.log('Function processing completed');
});
Attaches callbacks for the resolution and/or rejection of the Promise.
A Promise for the completion of which ever callback is executed.
Attaches callbacks for the resolution and/or rejection of the Promise.
A Promise for the completion of which ever callback is executed.
Attaches callbacks for the resolution and/or rejection of the Promise.
A Promise for the completion of which ever callback is executed.
Static
allReturns a single asynchronous Promise instance that resolves to an array of the results from the input promises.
This returned promise will resolve and execute it's pending chained operations asynchronously using the optional
provided timeout value to schedule when the chained items will be executed, or if the input contains no promises.
It rejects immediately upon any of the input promises rejected or non-promises throwing an error,
and will reject with this first rejection message / error.
When resolved or rejected any additional chained operations will execute asynchronously using the optional
timeout value to schedul when the chained item will be executed (eg. then()
; catch()
; finally()
).
The array of promises to wait to be resolved / rejected before resolving or rejecting the new promise
Optional
timeout: numberOptional timeout to wait before processing the items, defaults to zero.
Static
allReturns a single Promise instance that resolves to an array of the results from the input promises. This returned promise will resolve and execute it's pending chained operations based on the Aasynchronous promise implementation. Any chained operations will execute asynchronously when the final operation pending promises have resolved, or if the input contains no promises. It will resolve only after all of the input promises have either resolved or rejected, and will resolve with an array of IPromiseResult objects that each describe the outcome of each promise.
An array of promises to wait to be resolved / rejected before resolving or rejecting the new promise
Optional
timeout: numberOptional timeout to wait before processing the items, defaults to zero, only used when Native promises are not available.
A pending Promise
that will resolve to an array of IPromiseResult objects that each describe the outcome of each promise.
Returns a single Promise instance that resolves to an array of the results from the input promises. This returned promise will resolve and execute it's pending chained operations based on the Aasynchronous promise implementation. Any chained operations will execute asynchronously when the final operation pending promises have resolved, or if the input contains no promises. It will resolve only after all of the input promises have either resolved or rejected, and will resolve with an array of IPromiseResult objects that each describe the outcome of each promise.
A pending Promise
that will resolve to an array of IPromiseResult objects that each describe the outcome of each promise.
Static
anyThe createAsyncAnyPromise
method takes an iterable of promises as input and returns a single Promise.
This returned promise fulfills when any of the input's promises fulfills, with this first fulfillment value.
It rejects when all of the input's promises reject (including when an empty iterable is passed), with an
AggregateError containing an array of rejection reasons.
A new Promise that is:
The createAsyncAnyPromise
method takes an iterable of promises as input and returns a single Promise.
This returned promise fulfills when any of the input's promises fulfills, with this first fulfillment value.
It rejects when all of the input's promises reject (including when an empty iterable is passed), with an
AggregateError containing an array of rejection reasons.
An iterable object of promises.
Optional
timeout: numberOptional timeout to wait before processing the items, defaults to zero, only used when Native promises are not available.
A new Promise that is:
Static
raceThe createAsyncRacePromise
method takes an array of promises as input and returns a single Promise. This returned promise
settles with the eventual state of the first promise that settles.
A Promise that settles with the eventual state of the first promise in the iterable to settle. In other words, it fulfills if the first promise to settle is fulfilled, and rejects if the first promise to settle is rejected. The returned promise remains pending forever if the iterable passed is empty. If the iterable passed is non-empty but contains no pending promises, the returned promise is still asynchronously settled.
The createAsyncRacePromise
method is one of the promise concurrency methods. It's useful when you want the first
async task to complete, but do not care about its eventual state (i.e. it can either succeed or fail).
If the iterable contains one or more non-promise values and/or an already settled promise, then Promise.race() will settle to
the first of these values found in the iterable.
The createAsyncRacePromise
method takes an array of promises as input and returns a single Promise. This returned promise
settles with the eventual state of the first promise that settles.
An iterable object of promises.
Optional
timeout: numberOptional timeout to wait before processing the items, defaults to zero, only used when Native promises are not available.
A Promise that settles with the eventual state of the first promise in the iterable to settle. In other words, it fulfills if the first promise to settle is fulfilled, and rejects if the first promise to settle is rejected. The returned promise remains pending forever if the iterable passed is empty. If the iterable passed is non-empty but contains no pending promises, the returned promise is still asynchronously settled.
The createAsyncRacePromise
method is one of the promise concurrency methods. It's useful when you want the first
async task to complete, but do not care about its eventual state (i.e. it can either succeed or fail).
If the iterable contains one or more non-promise values and/or an already settled promise, then Promise.race() will settle to
the first of these values found in the iterable.
Static
rejectReturns a single asynchronous Promise instance that is already rejected with the given reason.
Any chained operations will execute asynchronously using the optional timeout value to schedule
when then chained items will be executed. (eg. catch()
; finally()
).
Optional
reason: anyThe rejection reason
Optional
timeout: numberOptional timeout to wait before processing the items, defaults to zero.
A rejected promise.
Static
resolveReturns a single asynchronous Promise instance that is already resolved with the given value. If the value passed is
a promise then that promise is returned instead of creating a new asynchronous promise instance.
If a new instance is returned then any chained operations will execute asynchronously using the optional
timeout value to schedule when the chained items will be executed.(eg. then()
; finally()
).
A resolved promise.
Returns a single asynchronous Promise instance that is already resolved with the given value. If the value passed is
a promise then that promise is returned instead of creating a new asynchronous promise instance.
If a new instance is returned then any chained operations will execute asynchronously using the optional
timeout value to schedule when the chained items will be executed.(eg. then()
; finally()
).
A resolved promise.
A full polyfill for the Promise class. Represents the completion of an asynchronous operation, and its resulting value.
Since
0.5.0