Solution Review: Print Numbers Sequentially
Explore how to print numbers in sequential order in JavaScript despite varying asynchronous delays. Understand the use of async functions and the await keyword to ensure promises resolve one after another. This lesson helps you master controlling asynchronous execution flow with callbacks and promises.
We'll cover the following...
We'll cover the following...
Solution
Explanation
Let’s look at the sleep function first.
const sleep = (i,ms) => new Promise(resolve => setTimeout(() => resolve(i), ms));
It takes two parameters: i and ms, the latter of which is the time in milliseconds. It returns a promise that resolves with value i after the specified milliseconds (ms) have passed. This is achieved using the setTimeout ...