tinuggunit
tinuggunit

Reputation: 39

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?

Upvotes: 0

Views: 47

Answers (1)

Dirk V
Dirk V

Reputation: 1469

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);

});

Upvotes: 1

Related Questions