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.
Create a Promise object that represents the eventual completion (or failure) of an asynchronous operation and its resulting value. This interface definition, closely mirrors the typescript / javascript PromiseLike and Promise definitions as well as providing
simular functions as that provided by jQuery deferred objects.
The returned Promise is a proxy for a value not necessarily known when the promise is created. It allows you to associate handlers with an asynchronous action's eventual success value or failure reason. This lets asynchronous methods return values like synchronous methods: instead of immediately returning the final value, the asynchronous method returns a promise to supply the value at some point in the future.
A Promise is in one of these states:
A pending promise can either be fulfilled with a value or rejected with a reason (error). When either of these options happens, the associated handlers queued up by a promise's then method are called synchronously. If the promise has already been fulfilled or rejected when a corresponding handler is attached, the handler will be called synchronously, so there is no race condition between an asynchronous operation completing and its handlers being attached.
As the
then()
andcatch()
methods return promises, they can be chained.