...
/Solution Review: Print Numbers Sequentially
Solution Review: Print Numbers Sequentially
This lesson will explain the solution to the problem in the previous lesson.
We'll cover the following...
Solution #
Press + to interact
const sleep = (i,ms) => new Promise(resolve => setTimeout(() => resolve(i), ms));async function print() {for (let i = 0; i < 10; i++) {await sleep(i,Math.random()*1000).then(function(result){console.log(result)})}}print()
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 ...