按序列运行Promise [英] Running Promises in array in series

查看:127
本文介绍了按序列运行Promise的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个链接数组,但是并行地执行它们会使服务器挂起并超时。

  var pages = linksArray.then(function(arr){
return arr.map(function(link){
return loader(link);
});
})。then (函数(数据){
console.log(data);
返回数据;
});

如何加载与链接数组关联的页面? loader是一个获取html的承诺解决方案

运行Promise数组的最常用方法是使用<$ c
$ b pre $ var $ pages = linkArray.then(function(arr ){
var pArray = [];
return arr.reduce(function(promise,link){
var ret = promise.then(function(){
return loader链接)
//接下来的3行将确保所有的链接处理 - 任何失败的链接将解析一个值== false
.catch(function(err){
return false;
$);
pArray.push(ret);
//接下来的三行记录每个加载器完成
ret.then(function() {
console.log('finished',link);
});
return ret;
},Promise.resolve())
.then (){
return Promise.all(pArra y);
});
))

然后您可以像这样访问结果

$ b $ (数据){
//数据是加载程序的结果数组
console.log(data)
pre $ ;
))。catch(function(err){//任何错误都应该记录在这里
console.log(err);
});




工作原理:简单地说,每次调用 loader 等待,直到之前的调用被解析,然后执行 - 每个加载器的有效承诺保存在一个数组中。一旦最后一个加载器解析完成, Promise.all 返回一个Promise,它解析为每个调用的值数组 loader

如果你需要知道array.reduce是如何工作的 - 阅读这个



I have an array of links, but executing them in parallel like this makes the server hang up and time out

var pages = linksArray.then(function(arr){
    return arr.map(function(link) {
              return loader(link);
              });
          }).then(function(data){
            console.log(data);
            return data;
          });

How can I load the pages that are associated with the array of links, in series? loader is a promise that gets the html

解决方案

the most common way of running an array of Promises in series is using array.reduce - like so

var pages = linksArray.then(function (arr) {
    var pArray = [];
    return arr.reduce(function (promise, link) {
        var ret = promise.then(function() {
            return loader(link)
             // the next 3 lines will ensure all links are processed - any failed links will resolve with a value == false
            .catch(function(err) {
                return false;
            });
        });
        pArray.push(ret);
        // next three lines log when each loader has finished
        ret.then(function() {
            console.log('finished', link);
        });
        return ret;
    }, Promise.resolve())
    .then(function() {
        return Promise.all(pArray);
    });
})

You can then access the results like this

pages.then(function (data) {
    // data is an array of results of loader
    console.log(data);
}).catch(function(err) { // any errors should be logged here
    console.log(err);
});

How it works: simply, each call to loader "waits" until the previous call is resolved before being executed - the effective promise for each loader is saved in an array. Once the last loader resolves, the Promise.all returns a Promise which resolves to an array of values of each of the calls to loader

If you need to know how array.reduce works - read this

这篇关于按序列运行Promise的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆