To do so, the subclass's constructor must implement the same signature as the Promise() constructor - accepting a single executor function that can be called with the resolve and reject callbacks as parameters. They all support subclassing, which means they can be called on subclasses of Promise, and the result will be a promise of the subclass type. In other words, fulfills when any of the promises fulfills rejects when any of the promises rejects.Īll these methods take an iterable of promises ( thenables, to be exact) and return a new promise. Settles when any of the promises settles. Promise.any()įulfills when any of the promises fulfills rejects when all of the promises reject. Promise.allSettled()įulfills when all promises settle. The Promise class offers four static methods to facilitate async task concurrency: Promise.all()įulfills when all of the promises fulfill rejects when any of the promises rejects. For the following code, the transition of promiseA into a "settled" state will cause both instances of. The resulting nesting would look like this: (promise D, (promise C, (promise X) ) )Ī promise can participate in more than one nesting. then() associated with "promise B" returns a nextValue of "promise X". For the nesting shown above, suppose the. The return causes a promise to be popped, but the nextValue promise is pushed into its place. When a nextValue is a promise, the effect is a dynamic replacement. (promise D, (promise C, (promise B, (promise A) ) ) ) The first promise in the chain is most deeply nested and is the first to pop. The promises of a chain are nested in one another, but get popped like the top of a stack. then(), while the reason for rejection is passed along to the next rejection-handler function in the chain. The return value of each fulfilled promise in the chain is passed along to the next. A "fulfilled" state indicates a successful completion of the promise, while a "rejected" state indicates a lack of success. The termination condition of a promise determines the "settled" state of the next promise in the chain. Note: For faster execution, all synchronous actions should preferably be done within one handler, otherwise it would take several ticks to execute all handlers in sequence. Colloquially, "resolved" promises are often equivalent to "fulfilled" promises, but as illustrated in "States and fates", resolved promises can be pending or rejected as well. The States and fates document from the original Promise proposal contains more details about promise terminology. You will also hear the term resolved used with promises - this means that the promise is settled or "locked-in" to match the eventual state of another promise, and further resolving or rejecting it has no effect. If the promise has already been fulfilled or rejected when a corresponding handler is attached, the handler will be called, so there is no race condition between an asynchronous operation completing and its handlers being attached.Ī promise is said to be settled if it is either fulfilled or rejected, but not pending. When either of these options occur, the associated handlers queued up by a promise's then method are called. The eventual state of a pending promise can either be fulfilled with a value or rejected with a reason (error). rejected: meaning that the operation failed. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |