Categories
JavaScript Answers

How to write asynchronous functions for Node.js?

Spread the love

Sometimes, we want to write asynchronous functions for Node.js.

In this article, we’ll look at how to write asynchronous functions for Node.js.

How to write asynchronous functions for Node.js?

To write asynchronous functions for Node.js, we can use promises.

For instance, we write

const ace = async () => {
  const r = await new Promise((resolve, reject) => {
    resolve(true);
  });

  console.log(r);
};

to create the ace async function that invokes a promise.

We create the promise with the Promise constructor called with a callback that calls resolve with the resolve value of the promise.

We get the promise’s resolve value with await.

Conclusion

To write asynchronous functions for Node.js, we can use promises.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *