I am learning about Javascript ES2017 async/await feature. Been reading a lot about it, and came across an understanding that await is like yield, and allows us to wait for the promises to complete.
From https://javascript.info/async-await
async function showAvatar() {
// read our JSON
let response = await fetch('/article/promise-chaining/user.json');
let user = await response.json();
// read github user
let githubResponse = await fetch(`https://api.github.com/users/${user.name}`);
let githubUser = await githubResponse.json();
// show the avatar
let img = document.createElement('img');
img.src = githubUser.avatar_url;
img.className = "promise-avatar-example";
document.body.append(img);
// wait 3 seconds
await new Promise((resolve, reject) => setTimeout(resolve, 3000));
img.remove();
return githubUser;
}
showAvatar();
The question I have is, can I add await to every single line of code? Or what happens if I remove the word await?
And another question is, if async/ await makes the code seems to run synchronously and in order, why don't we don't use it at all (means make everything stay synchronous in the first place?)
Thank you!
async
functions are just syntactic sugar around Promises. It does nothing to change the function to be synchronous. In fact any function that isasync
implicitly returns aPromise
object.All
await
does is provide a convenient way to wait for a Promise. If you remove theawait
keyword, then thePromise
will not be "unwrapped," which is not what you want if your function is going to operate on the result of that Promise.To illustrate, this is the desugared version of your async function:
So
await
essentially just adds a.then
callback to a Promise. Withoutawait
being specified, you'll just have a Promise object, not the result of the Promise.await
tells the runtime to wait for the promise returned from the expression on the right-hand side to be fulfilled.In the following code control is stopped in the async function until the promise returned by the
fetch
invocation is fulfilled, and the response value sent to the fulfilled promise is printed to the console.If the await keyword was omitted then control would immediately continue to the next line of the function and the the promise returned by fetch would be immediately printed to the console.
await
is a contextual keyword that only means something special when used inside a function markedasync
. By marking a functionasync
you are telling the runtime to:yield
a value where the user typesawait
(usually a promise)In this way, asynchronous control flows can be written in a style closer to the traditional synchronous style. ie. without nesting, callbacks or visible promises.
try..catch
can also be used in the normal way.As another answer mentions,
async
, andawait
are syntactic sugar that ask the runtime to use existing objects (generator functions and Promises) behind the scenes to make async code easier to read and write.I would guess you can. If the expression on the right of the
await
results in a promise, then the async/await behaviour detailed above occurs.If the expression on the right of the
await
does not result in a promise, then I would guess the value is wrapped in a resolved promise for you, and logic continues per the above, as though the value came from an immediately resolved promise. This is a guess.First,
async
/await
, work with promises. If you are not calling from anasync
function or theawait
sentence is notthen
compatible, it won't work. In most cases if you removeawait
, you are going to end with aPromise
and not the value of the promise.Secondly, there are some cases that you may want to continue with the execution of the code while you are resolving some asynchronous task.