In the following code, what I'm after is to get the alert, after the counting is finished in the setInterval. The alert shows first though, and then the counting happens. What have I done wrong?
function function1() {
return new Promise((resolve, reject) => {
console.log('in function1 ')
function2().then(resolve());
});
}
function function2() {
return new Promise((resolve, reject) => {
var x = 0;
I = setInterval(function() {
console.log(x);
if (x > 10) {
clearInterval(I);
resolve()
};
x++
}, 100);
});
}
function1().then(alert('finished'));