O'Reilly logo

Hands-On Microservices with Node.js by Diogo Resende

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Promises and async/await

A Promise is an object that represents the completion or failure of an asynchronous operation. The Promise can be chained to perform serial operations, run in parallel until all operations execute, or even race operations and wait only for the first completion or failure:

Promise.race([    new Promise((resolve, reject) => {        // some possibly long operation    }),    new Promise((resolve, reject) => {        setTimeout(reject, 5000);    })]).then(() => {    console.log("success!");}, () => {    console.log("failed");});

More recently, a syntactic sugar was created around Promise to mimic synchronous code syntax. Basically, you can indicate that a function is asynchronously using the async keyword. The function will then return a Promise when ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required