简体   繁体   中英

Asynchronous responses in Node.js

I am working with express. I want to send the response as the output of an asynchronous function. Something like this:

app.get('/', (req, res) => {

  functionResponse = asynchronousFunction();
  res.send(functionResponse);

});

How do I achieve this?

You can do this by using an async function . Here is the example applied to your code. It will wait for the asynchronous function to finish before continuing.

app.get('/', async (req, res) => {

  functionResponse = await asynchronousFunction();
  res.send(functionResponse);

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM