I think my understanding of it might be affected by my experience with .NET's async/await
, so I'd like some code example:
I'm trying to make a express controller wait 5 seconds before returning the response:
const getUsers = async (ms) => {
var wait = ms => new Promise(resolve => setTimeout(resolve, ms));
await wait(ms);
};
export const index = (req, res) => {
async () => {
await getUsers(5000);
res.json([
{
id: 1,
name: 'John Doe',
},
{ id: 2,
name: 'Jane Doe',
},
]);
};
};
This code doesn't work, the browser keeps loading and loading and never shows a thing.
The getUser
function I built based on this SO answer, and the controller method, based on my (mistaken) understanding of how it works so I'd like some clarification and correction:
1. when should I use await
?
To my understanding, you should use await
before an async
function call. Is this correct? Also, why can I call await before a non-async function that returns a promise?
2. When should I use async
?
To my understanding, you mark a function as an async
one so that it can be called with the await
keyword. Is this correct? Also, [why] do I have to wrap my await getUsers(5000)
call in an anonymous async function?
An async function is a function declared with the async keyword, and the await keyword is permitted within it. 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.
With Node v8, the async/await feature was officially rolled out by the Node to deal with Promises and function chaining. The functions need not to be chained one after another, simply await the function that returns the Promise. But the function async needs to be declared before awaiting a function returning a Promise.
1. Promise is an object representing intermediate state of operation which is guaranteed to complete its execution at some point in future. Async/Await is a syntactic sugar for promises, a wrapper making the code execute more synchronously.
They keyword async is used to make a function asynchronous. The await keyword will ask the execution to wait until the defined task gets executed. It allows the use of await Keyword inside the functions with async keyword. Using await in any other way will cause a syntax error.
To clear a few doubts -
await
with any function which returns a promise. The function you're awaiting doesn't need to be async
necessarily.async
functions when you want to use the await
keyword inside that function. If you're not gonna be using the await
keyword inside a function then you don't need to make that function async
.async
functions by default return a promise. That is the reason that you're able to await
async
functions. From MDN -
When an async function is called, it returns a Promise.
As far as your code is concerned, it could be written like this -
const getUsers = (ms) => { // No need to make this async
return new Promise(resolve => setTimeout(resolve, ms));
};
// this function is async as we need to use await inside it
export const index = async (req, res) => {
await getUsers(5000);
res.json([
{
id: 1,
name: 'John Doe',
},
{ id: 2,
name: 'Jane Doe',
},
]);
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With