My Understanding towards Promise/A+

Personal comprehension about ES6 promise,including simulated promise implementation

For a stupid Instance

There’s a scene that you’re going to order a cup of coffee.The pretty waitress may say ‘Please come to get your coffee later’.So this is a

Promise
.She promised that you can get a coffee soon.Besides, your waiting process is called
pending
.After you sucessfully get your coffee from the waitress,this’s called
resolved(fulfilled)
.But it’s possible that her coffee beans are out of stock.As a result you can’t get your coffee,so this is
rejected
.Furthermore,after you got the promise,the following operation is called
then
;

Definition

The Promise object represents the eventual completion (or failure) of an asynchronous operation, and its resulting value.MDN

Essentially, a promise is a returned object to which you attach callbacks, instead of passing callbacks into a function.(That’s why it can avoid callback hell,once you call then() method,it will implicitly

create a new promise object and return it;
) MDN

More advantages:

Unlike old-style passed-in callbacks, a promise comes with some guarantees:

1. Callbacks will never be called before the completion of the current run of the JavaScript event loop.
2. Callbacks added with .then even after the success or failure of the asynchronous operation, will be called, as above.
3. Multiple callbacks may be added by calling .then several times, to be executed independently in insertion order.

But the most immediate benefit of promises is chaining.

For more detail:Promise A+

Following my code about a simple promise implementation:


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!