Promise.all - 即使其中一部分被拒绝,也等待所有人解决

时间:2016-05-18 06:19:54

标签: javascript node.js promise ecmascript-6 es6-promise

我有一个Promise数组,我想并行执行。

  1. 我想等待所有人在做某事之前解决问题
  2. 在他们全部解决之后,我想采取一些行动
  3. 我想知道他们中的任何一个是否被拒绝。然而,即使有些承诺被拒绝,我仍然希望其他承诺得以实现。

    希望在ES6中使用内置的Promise,而不是使用Q,When,WinJS,CO等任何特殊库。

    这是具体的例子。在这里,我想知道round1中的所有8个URL都已完成(2个失败,6个成功),然后再转到第2轮中的URL。

    当所有承诺被拒绝时,会发生什么事情,控制权移动到第一个捕获区并继续通过密码链。

    "use strict";
    
    var request = require('request');
    var util = require('util');
    
    function getPage(url){
        return new Promise(function(resolve,reject){
            request(url, function(error, response, body){
                if(!error && response.statusCode == 200){
                    let result = util.format('Done with %s, %d bytes', url, body.length);
                    console.log(result);
                    resolve(result);
                }
                else{
                    reject(Error(error));
                }
            })
        });
    }
    
    let round1 = ['http://www.google.com', 'http://www.yahoo.com', 'http://www.nytimes.com', 'http://www.wsj.com', 'http://www.bad1ffsdwre.com', 'http://www.cnn.com', 'http://www.bad2ffsdwre.com', 'http://www.msnbc.com'];
    let round2 = ['http://www.facebook.com', 'http://www.twitter.com', 'http://www.snapchat.com', 'http://www.instagram.com'];
    
    Promise.all(round1.map(getPage))
        .then(function(results){
            console.log('Successfully completed Round1');       
        })
        .catch(function(error){
            console.log('there is an error in round 1: ', error);       
        })
        .then(function(){
            console.log('Done with Round 1')
        })
        .then(function(){
            Promise.all(round2.map(getPage))
                .then(function(results){
                    console.log('Done with Round2');        
                })
                .catch(function(error){
                    console.log('there is an error in round 2', error);     
                })          
        });
    

    实际输出:

    node promises.js                                                                    
    there is an error in round 1:  [Error: Error: getaddrinfo ENOTFOUND www.bad1ffsdwre.com www.bad1ffsdwre.com:80]    
    Done with Round 1                                                                                                  
    Done with http://www.google.com, 50085 bytes                                                                       
    Done with http://www.cnn.com, 106541 bytes                                                                         
    Done with http://www.snapchat.com, 6320 bytes                                                                      
    Done with http://www.instagram.com, 14707 bytes                                                                    
    Done with http://www.msnbc.com, 139798 bytes                                                                       
    Done with http://www.facebook.com, 34710 bytes                                                                     
    Done with http://www.nytimes.com, 177062 bytes                                                                     
    Done with http://www.wsj.com, 827412 bytes                                                                         
    Done with http://www.yahoo.com, 632892 bytes                                                                       
    Done with http://www.twitter.com, 260178 bytes                                                                     
    Done with Round2 
    

    期望的输出:

    node promises.js                                                                    
    there is an error in round 1:  [Error: Error: getaddrinfo ENOTFOUND www.bad1ffsdwre.com www.bad1ffsdwre.com:80]    
    Done with http://www.google.com, 50085 bytes                                                                       
    Done with http://www.cnn.com, 106541 bytes                                                                         
    Done with http://www.msnbc.com, 139798 bytes                                                                       
    Done with http://www.nytimes.com, 177062 bytes                                                                     
    Done with http://www.wsj.com, 827412 bytes                                                                         
    Done with http://www.yahoo.com, 632892 bytes
    there is an error in round 1:  [Error: Error: getaddrinfo ENOTFOUND www.bad2ffsdwre.com www.bad2ffsdwre.com:80]
    Done with Round 1 
    Done with http://www.snapchat.com, 6320 bytes                                                                      
    Done with http://www.instagram.com, 14707 bytes                                                                    
    Done with http://www.facebook.com, 34710 bytes                      
    Done with http://www.twitter.com, 260178 bytes                                                                     
    Done with Round2 
    

    我正在努力学习这个very excellent article中概述的普通ES6 Promises中的模式。在本文的最后几位,作者还在这句话中提到了类似的东西。

      

    注意:我不相信Promise.race的用处;我宁愿有一个   与Promise.all相反,只有在所有项目都拒绝的情况下才会拒绝。

0 个答案:

没有答案