I've been in some technical interviews, this one got me frustrated as I can't modify the existent function and need to print 5 consecutive numbers (Number: 1, Number: 2, Number: 3....) plus the Promise
console.log('Logging Num 1');
const printNumber = (number) => {
return new Promise((resolve) => {
setTimeout(() => {
console.log(`Number: ${number}`);
resolve();
}, Math.floor(Math.random() * 10));
});
};
printNumber(1);
printNumber(2);
This was the hint, I remembered this one for looping through elements
for(let i = 1; i <= 5; i+= 1)
But how can I add this in the code without modifying the function? Yes I got nervous and confused.
You should be able to do this
const syncLog = async (x) => {
for(let i = 0; i < x; i++){
await printNumber(i)
}
}
syncLog(5)
>> Number: 0
>> Number: 1
>> Number: 2
>> Number: 3
>> Number: 4