async await Flashcards
What do the async and await keywords enable?
The async and await keywords enable asynchronous, promise-based behavior to be written in a cleaner style, avoiding the need to explicitly configure promise chains.
Consider the following code. What is the equivalent without using the async keyword?
async function foo() {
return 1;
}
function foo() {
return Promise.resolve(1);
}
Will an async function without an await expression run synchronously.
Yes it will.
If there is an await expression inside the function body will an async function always complete asynchronously?
yes, If there is an await expression inside the function body, the async function will always complete asynchronously.
What is a good way to think about code after each await expression?
Code after each await expression can be thought of as existing in a .then callback. In this way a promise chain is progressively constructed with each reentrant step through the function. The return value forms the final link in the chain.
Are async function declarations hoisted?
async function declarations are hoisted to the top of their scope and can be called anywhere in their scope.
What are asynchronous actions?
Actions that we initiate now, but they finish later.
What is the constructor syntax for a Promise object?
let promise = new Promise(function(resolve, reject) {
// executor (the producing code, “singer”)
});
What is the function passed to a new promise called?
The function passed to new Promise is called the executor.
What should the code in the executor function eventually produce ?
The code which should eventually produce the result.
What should the code in the executor function eventually produce ?
The code which should eventually produce the result.
What arguments are passed to the executor function. Who provides them?
Its arguments resolve and reject are callbacks provided by JavaScript itself. Our code is only inside the executor.
When the executor obtains the result, it should call one of two callbacks, what are they?
When the executor obtains the result, be it soon or late, doesn’t matter, it should call one of these callbacks:
resolve(value) — if the job is finished successfully, pass value as the result.
reject(error) — if an error has occurred, error is the error object.
Summarize the executors behavior and purpose?
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 successful or reject if there was an error.
The promise object returned by the new Promise constructor has what internal properties? Describe them.
state — initially “pending”, then changes to either “fulfilled” when resolve is called or “rejected” when reject is called.
result — initially undefined, then changes to value when resolve(value) is called or error when reject(error) is called.
A promise that is either resolved or rejected is called what? What is the initial state of a promise called?
A promise that is either resolved or rejected is called “settled”, as opposed to an initially “pending” promise.
Can the executor call more than one resolve or reject?
No there can be only a single result or an error
The executor should call only one resolve or one reject. Any state change is final.
let promise = new Promise(function(resolve, reject) {
resolve(“done”);
reject(new Error(“…”)); // ignored
setTimeout(() => resolve(“…”)); // ignored
});
How many arguments do resolve/reject accept?
Also, resolve/reject expect only one argument (or none) and will ignore additional arguments.
In case something goes wrong, the executor should call reject. That can be done with any type of argument (just like resolve). What type of argument is recommended to be used?
It is recommended to use Error objects (or objects that inherit from Error).
Can you immediately resolve/reject in an executor?
In practice, an executor usually does something asynchronously and calls resolve/reject after some time, but it doesn’t have to. We also can call resolve or reject immediately, like this:
let promise = new Promise(function(resolve, reject) {
// not taking our time to do the job
resolve(123); // immediately give the result: 123
});
Can you directly access the properties “state” and “result” of a Promise object?
No, the properties state and result of the Promise object are internal. We can’t directly access them. We can use the methods .then/.catch/.finally for that.
The .then( ) method of a Promise accepts two arguments, what are they?
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.
In an executor how would you pass an Error as the reject value?
reject(new Error(“Whoops!”))
Do we need to pass both arguments to .then() ?
If we’re interested only in successful completions, then we can provide only one function argument to .then:
let promise = new Promise(resolve => {
setTimeout(() => resolve(“done!”), 1000);
});
promise.then(alert); // shows “done!” after 1 second
What is the Promise .catch( ) method the short hand for ?
The call .catch(f) is a complete analog of .then(null, f).
What is the call .finally(f) similar to and why?
The call .finally(f) is similar to .then(f, f) in the sense that f runs always, when the promise is settled: be it resolve or reject.
What arguments does a .finally( ) handler accept?
A finally handler has no arguments. In finally we don’t know whether the promise is successful or not. That’s all right, as our task is usually to perform “general” finalizing procedures.
Does a Finally handler pass the result or error to the next suitable handler?
Yes, A finally handler “passes through” the result or error to the next suitable handler.
new Promise((resolve, reject) => {
setTimeout(() => resolve(“value”), 2000);
})
.finally(() => alert(“Promise ready”)) // triggers first
.then(result => alert(result)); // <– .then shows “value”
Should a finally handler return anything?
No, A finally handler also shouldn’t return anything. If it does, the returned value is silently ignored.
The only exception to this rule is when a finally handler throws an error. Then this error goes to the next handler, instead of any previous outcome.