442   JS

const runTimeout = function(ms){
    var p = new Promise(function(resolve, reject){
        setTimeout(function(){
            console.log('done');
            resolve('timeout : ' + ms);
        }, ms);
    });
    return p;            
}

const promise_timeout = [runTimeout(100),runTimeout(200),runTimeout(300)]
Promise.all(promise_timeout).then(function (response) {
  // 等到所有异步任务执行后才执行,必须所有执行
  console.log('done all')
}).catch(function(error){
  // ...
});

Promise.race(promise_timeout).then(function (response) {
  // 只要一个异步任务执行就执行,最快原则
  console.log('done race')
}).catch(function(error){
  // ...
}).finally(function(){
  // 最后都会执行
  console.log('done finally')
});
	



Leave a Reply

Your email address will not be published. Required fields are marked *