promises JS.
Promise - , , . , . .
Usually you have 3 functions that depend on each other. The first should wait for the second, and the second - to wait for the third. At the first level, the second function should return Promise.
function delayed1() {
does_something_here();
setTimeout(function () {
var promise = do_something1();
promise.then(function(){
if ( cond1 ){
delayed1();
}
});
}, 1000)
}
Here is your second function. He creates a promise and returns it. Also inside if ( cond3 )he must wait for a promise from the third function. I will not write my third function, as I am sure that you can do it yourself by following this pattern.
function do_something1(){
return new Promise(function(resolve){
if ( cond3 ){
var promise = delayed(2);
promise.then(function(){
resolve()
})
}
else {
something_else();
resolve();
}
})
}
source
share