Please enable JavaScript.
Coggle requires JavaScript to display documents.
Promises, async/await - Coggle Diagram
Promises, async/await
Promise
-
-
-
-
-
let promise = new Promise(function(resolve, reject) {}
reject(error) — if an error has occurred, error is the error object.
resolve(value) — if the job is finished successfully, with result value.
Consumers: then, catch
then
The first argument of .then is a function that runs when the promise is resolved and receives the result.
The second argument of .then is a function that runs when the promise is rejected and receives the error.
If we’re interested only in successful completions, then we can provide only one function argument to .then
catch
If we’re interested only in errors, then we can use null as the first argument: .then(null, errorHandlingFunction)
-
Cleanup: finally
The idea of finally is to set up a handler for performing cleanup/finalizing after the previous operations are complete.
A finally handler doesn’t get the outcome of the previous handler (it has no arguments). This outcome is passed through instead, to the next suitable handler.
When finally throws an error, then the execution goes to the nearest error handler.
If a finally handler returns something, it’s ignored.
-
-
Theory
-
define
object that may produce a single value some time in the future with either a resolved value or a reason that it’s not resolved
-
main rules of promise
-
-
-
Once a promise is settled, the value must not change.
promise chaining
The process of executing a sequence of asynchronous tasks one after another using promises is known as Promise chaining
Method
promise.all
takes an array of promises as an input (an iterable), and it gets resolved when all the promises get resolved or any one of them gets rejected.
-
-
-
Promise API
Promise.all
-
takes an iterable (usually, an array of promises) and returns a new promise.
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.
In case of an error, other promises are ignored
-
Promise.allSettled
waits for all promises to settle, regardless of the result.
{status:"fulfilled", value:result} for successful responses,
{status:"rejected", reason:error} for errors.
-
Promise.race
Similar to Promise.all, but waits only for the first settled promise and gets its result (or error).
The first promise here was fastest, so it became the result.
After the first settled promise “wins the race”, all further results/errors are ignored.
Promise.any
Similar to Promise.race, but waits only for the first fulfilled promise and gets its result.
If all of the given promises are rejected, then the returned promise is rejected with AggregateError
If have one promise resolve, all further results are ignored.
Promise.resolve/reject
-
-
Methods Promise.resolve and Promise.reject are rarely needed in modern code, because replacement of async/await
callbacks
understand
-
A function that does something asynchronously should provide a callback argument where we put the function to run after it’s complete.
Callback in callback
every new action is inside a callback. That’s fine for few actions, but not good for many
Handling errors
In case the operation does not go as expected, call back to assist with handling if there is an error
-
-
-
Microtasks
-
Unhandled rejection
An “unhandled rejection” occurs when a promise error is not handled at the end of the microtask queue.
we understand that unhandledrejection is generated when the microtask queue is complete: the engine examines promises and, if any of them is in the “rejected” state, then the event triggers.
Async/await
Async functions
The word “async” before a function means one simple thing: a function always returns a promise. Other values are wrapped in a resolved promise automatically.
-