Merge branch 'master' into patch-2

This commit is contained in:
Ilya Kantor 2020-01-19 16:26:35 +01:00 committed by GitHub
commit b9bb854c73
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
84 changed files with 713 additions and 694 deletions

View file

@ -8,7 +8,7 @@ Everyone is happy: you, because the people don't crowd you anymore, and fans, be
This is a real-life analogy for things we often have in programming:
1. A "producing code" that does something and takes time. For instance, a code that loads the data over a network. That's a "singer".
1. A "producing code" that does something and takes time. For instance, some code that loads the data over a network. That's a "singer".
2. A "consuming code" that wants the result of the "producing code" once it's ready. Many functions may need that result. These are the "fans".
3. A *promise* is a special JavaScript object that links the "producing code" and the "consuming code" together. In terms of our analogy: this is the "subscription list". The "producing code" takes whatever time it needs to produce the promised result, and the "promise" makes that result available to all of the subscribed code when it's ready.
@ -22,7 +22,7 @@ let promise = new Promise(function(resolve, reject) {
});
```
The function passed to `new Promise` is called the *executor*. When `new Promise` is created, it runs automatically. It contains the producing code, that should eventually produce a result. In terms of the analogy above, the executor is the "singer".
The function passed to `new Promise` is called the *executor*. When `new Promise` is created, the executor runs automatically. It contains the producing code which should eventually produce the result. In terms of the analogy above: the executor is the "singer".
Its arguments `resolve` and `reject` are callbacks provided by JavaScript itself. Our code is only inside the executor.
@ -31,7 +31,7 @@ When the executor obtains the result, be it soon or late - doesn't matter, it sh
- `resolve(value)` — if the job finished successfully, with result `value`.
- `reject(error)` — if an error occurred, `error` is the error object.
So to summarize: the executor runs automatically and attempts to perform a job. When it is finished with the attempt it calls `resolve` if it was succssful or `reject` if there was an error.
So to summarize: the executor runs automatically and performs a job. Then it should call `resolve` if it was succssful or `reject` if there was an error.
The `promise` object returned by the `new Promise` constructor has internal properties:
@ -272,6 +272,10 @@ let promise = new Promise(resolve => resolve("done!"));
promise.then(alert); // done! (shows up right now)
```
Note that this is different, and more powerful than the real life "subscription list" scenario. If the singer has already released their song and then a person signs up on the subscription list, they probably won't receive that song. Subscriptions in real life must be done prior to the event.
Promises are more flexible. We can add handlers any time: if the result is already there, our handlers get it immediately.
````
Next, let's see more practical examples of how promises can help us write asynchronous code.