You don't need the "then" since the await do the job.
Also you don't need the next() at the end, if I'm not in a mistake, async express just go next when finish solving all the promises inside the function.
I'm in the mobile now but try to put an example later if you want
const endpoint = 'jsonplaceholder.typicode.com/posts/1';
const asyncMiddleware = async (req,res) => {
const data = await PromiseBasedDataRequest(endpoint);
req.data = await data.json() // if I remember well data.json() its a
//promise too thats why about another
//await
return req.data
}
// the workflow continues
Async await it's almost have a "syncronous" code.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
You don't need the "then" since the await do the job.
Also you don't need the next() at the end, if I'm not in a mistake, async express just go next when finish solving all the promises inside the function.
I'm in the mobile now but try to put an example later if you want
Thanks! I updated my snippets.
Regarding the
next()
call, I'll have to check that out.I mean this:
const endpoint = 'jsonplaceholder.typicode.com/posts/1';
const asyncMiddleware = async (req,res) => {
const data = await PromiseBasedDataRequest(endpoint);
req.data = await data.json() // if I remember well data.json() its a
//promise too thats why about another
//await
return req.data
}
// the workflow continues
Async await it's almost have a "syncronous" code.