Here is an example. The console displays the order of such

1.1 1.2 -завершились- 3.1 3.2 

And you need to

-to complete-

displayed at the end. The function Promise.all, I understand, launches promises.
And how to hang up waiting, so that only when all promises are fulfilled does it work?

https://jsfiddle.net/aefuqvkL/

 var promises = []; var pr = new Promise(function(resolve, reject) { console.log('1.1'); resolve(true); }); var pr2 = new Promise(function(resolve, reject) { console.log('1.2'); resolve(true); }); promises.push(pr); promises.push(pr2); Promise.all(promises).then(values => { console.log('-завершились-'); }); setTimeout(function() { pr.then(function() { console.log('3.1'); }, function() { console.log('4.1'); }); }, 1000); setTimeout(function() { pr2.then(function() { console.log('3.2'); }, function() { console.log('4.2'); }); }, 3000); 
  • if you are only waiting for the first two Promise, why do you think you should wait for the remaining two? Notice that the then function returns a new Promise - Grundy
  • Well, what should I use to get my function called after 2 timeouts expire? That is, I need it. Know that both timeouts have expired. - manking

1 answer 1

Since now only two promises of pr and pr2 are transmitted to Promise.all , the conclusion is completely logical.

Function Promise.all I understand it launches promises

Misunderstood. Promises are launched immediately when the designer is executed.

In essence, the following expressions are equivalent:

 Promise.resolve(true); new Promise(function(resolve){ resolve(true); }); 

To wait for the promises that are created inside setTimeout , you need to get them somehow.

  1. wrap the necessary functions in the Promise constructor as it is:

     var promise = new Promise(function(resolve,reject){ setTimeout(function() { pr.then(function() { console.log('3.1'); resolve(true); }, function() { console.log('4.1'); reject(false); }); }, 1000); }); 

    and similarly with the second setTimeout, and already these two promises to transfer to Promise.all

  2. make a setTimeout wrapper that returns a promise:

     function delay(timeout){ return new Promise(function(resolve){ setTimeout(resolve, timeout); }); } 

Then the code will look like this:

 function delay(timeout) { return new Promise(function(resolve) { setTimeout(resolve, timeout); }); } var pr = new Promise(function(resolve, reject) { console.log('1.1'); resolve(true); }); var pr2 = new Promise(function(resolve, reject) { console.log('1.2'); resolve(true); }); var timeout1 = delay(1000).then(function() { return pr; }).then(function() { console.log('3.1'); }, function() { console.log('4.1'); }); var timeout2 = delay(3000).then(function() { return pr2; }).then(function() { console.log('3.2'); }, function() { console.log('4.2'); }); var promises = [timeout1, timeout2]; Promise.all(promises).then(values => { console.log('-завершились-'); }); 

  • Thank. But here's how everything is complicated. Although I just need to make a function call when both 1 and 2 timeout is completed. That is, synchronize the asynchronous code .. I thought in es6 there are some tools for this convenient. Or just like that? - manking
  • @manking, what exactly is the difficulty? you still create two promises and wait for them. Your problem was that you were not waiting for those promises - Grundy