I am new to JavaScript and was learning about promises. I want to print 5 to 0 numbers at the interval of 2 seconds each.
For example for the first time after 2 seconds print 5 and then after 2 second print 4 and so on.
I have written a code to do so but not able to get the correct output.
var count = 5;
var i = 2;
function d()
{
let p = new Promise((resolve,reject)=>{
while(count>=0)
{
setTimeout(resolve,i*1000,count);
i= i*2;
count = count-1;
//count = count-1;
}
});
return p;
}
d().then((x)=>{
console.log(x);
});
It is showing output as 5. Can anyone correct it?