Promise以及sync和async
For details, refer to Promises, async/await
Basic usage
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
As you can see, the value returned by the first promise is passed through finally
to the next then
.
Promise chain
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
then
should be a async
function, so it can convert any return values to a promise.
Error handling
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
The code of a promise executor and promise handlers has an “invisible” try..catch
around it. If an exception happens, it gets caught and treated as a rejection.
The “invisible” try..catch
around the executor automatically catches the error and turns it into rejected promise.
More details about the error handling workflow, refers to https://javascript.info/promise-error-handling
Promise.all
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
The new promise resolves when all listed promises are resolved, and the array of their results becomes its result.
If any of the promises is rejected, the promise returned by Promise.all
immediately rejects with that error.
Please note that the order of the resulting array members is the same as in its source promises. Even though the first promise takes the longest time to resolve, it’s still first in the array of results.
Promisification
Used for changing callback style to promise style.
async/await
1 2 3 4 5 |
|
async
ensures that the function returns a promise, and wraps non-promises in it.
1 2 3 4 5 6 7 8 9 10 11 12 |
|
The function execution “pauses” at the line (*) and resumes when the promise settles, with result becoming its result. So the code above shows “done!” in one second.
Let’s emphasize: await
literally suspends the function execution until the promise settles, and then resumes it with the promise result. That doesn’t cost any CPU resources, because the JavaScript engine can do other jobs in the meantime: execute other scripts, handle events, etc.
For details, refer to macrotask and microtask in event loop
Async/Sync
is just a more elegant syntax of getting the promise result than promise.then
. And, it’s easier to read and write.