From the course: Advanced Node.js

Unlock the full course today

Join today to access over 24,800 courses taught by industry experts.

Sequential execution with async/await

Sequential execution with async/await - Node.js Tutorial

From the course: Advanced Node.js

Sequential execution with async/await

- [Instructor] Promises give us a nice way to organize what happens by chaining together .then handlers. We use this technique to compose promise chains that can execute sequentially. But what if you don't want to compose functions in a chain? What happens when you just want to write code? All we really need to do is wait for the result of one promise to complete before running some more code. Do we always have to encapsulate code bits in tiny functions? The answer is no. JavaScript provides us a way to work with promises using code that appears synchronous, but is in fact asynchronous. The solution is async functions. Inside of an async function, you can await for a promise to complete. Let's look at how we can modify the current sequentially executed promise chain using async and await. So the doStuffSequentially function, to get started I'm just gonna go ahead and delete these last lines of code, the last .thens that we added to the doStuffSequentially function. And what I'm gonna…

Contents