如何以下Array.prototype.reduce累积回报承诺返回值作为数组。那么()()? [英] How to return accumulated returned Promise values as array to .then() following Array.prototype.reduce()?

查看:191
本文介绍了如何以下Array.prototype.reduce累积回报承诺返回值作为数组。那么()()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请注意,这个问题问得匆忙和管理不善此作者组成。 @ jfriend00的解决方案满足原始问题的要求。 @ Anonymous0days的解决方案满足在原来的问题和jfriend00的答案已经发布的注释后描述输入数组中拒绝承诺的预期结果描述的要求。

随后的评论交流让步了接受来自Anonymous0day的回答,荣获接受,以jfriend00的答案,保持提问的标题和提供的解决方案之间的一致性之前。


鉴于这种模式

  someArray.reduce(功能(P,项目){
  返回p.then(函数(){
    返回someFunction(项目);
  });
} $ .Deferred()解析()),然后(函数(){
  //全部在这里完成
  //累计访问履行,拒绝了`Promise`值
},功能ERR(){});

什么方法都可能返回的累积值满足,拒绝无极反对。然后(完成)作为数组下调用。降低()

\r
\r

函数someFunction(指数){\r
  的console.log(someFunction叫,索引=+指数);\r
  变量$递延= $ .Deferred();\r
\r
  window.setTimeout(函数(){\r
    $ deferred.resolve();\r
  },2000);\r
\r
  返回$ deferred.promise();\r
}\r
   \r
VAR =的someArray [1,2,3,4,5];\r
\r
someArray.reduce(功能(P,项目){\r
  返回p.then(函数(){\r
    返回someFunction(项目);\r
  });\r
} $ .Deferred()解析()),然后(功能(数据){\r
  //全部在这里完成\r
  的console.log(数据,参数)//`undefined`,`[]`\r
});

\r

<脚本SRC =htt​​ps://ajax.googleapis.com/ajax /libs/jquery/1.11.1/jquery.min.js\">\r
< / SCRIPT>

\r

\r
\r


解决方案

有根据的你试图做什么具体多种可能的策略:这里有一个选项:

  someArray.reduce(功能(P,项目){
  返回p.then(功能(阵列){
    返回someFunction(项目)。然后(功能(VAL){
        的Array.push(VAL);
        返回数组;
    });
  });
} $ .Deferred()。解析([]))。然后(功能(阵列){
  //全部在这里完成
  在阵列//积累的结果
},功能(错误){
  //犯错是从停止执行链中的承诺,拒绝错误
});

工作演示: http://jsfiddle.net/jfriend00/d4q1aaa0/


仅供参考,青鸟无极库(这是我一般用)具有 .mapSeries()这是专为这个模式:

  VAR =的someArray [1,2,3,4];Promise.mapSeries(的someArray,函数(项目){
    返回someFunction(项目);
}),然后(功能(结果){
    日志(结果);
});

工作演示: http://jsfiddle.net/jfriend00/7fm3wv7j/

Note, this Question was hastily and poorly composed by this author. @jfriend00's solution meets requirement of original Question. @Anonymous0days's solution meets requirements of description of expected results of rejected promises found in input array described at comments after original Question and jfriend00's Answer had already been posted.

Withdrew "accepted" from Anonymous0day's Answer , awarded "accepted" to jfriend00's Answer, to maintain consistency between title of Question and solutions provided prior to subsequent comment exchanges.


Given this pattern

someArray.reduce(function(p, item) {
  return p.then(function() {
    return someFunction(item);
  });
}, $.Deferred().resolve()).then(function() {
  // all done here
  // access accumulated fulfilled , rejected `Promise` values
}, function err() {

});

what approaches are possible to return accumulated values of fulfilled , rejected Promise objects to .then(fulfilled) as an array following call to .reduce() ?

function someFunction(index) {
  console.log("someFunction called, index = " + index);
  var $deferred = $.Deferred();

  window.setTimeout(function() {
    $deferred.resolve();
  }, 2000);

  return $deferred.promise();
}
   
var someArray = [1,2,3,4,5];

someArray.reduce(function(p, item) {
  return p.then(function() {
    return someFunction(item);
  });
}, $.Deferred().resolve()).then(function(data) {
  // all done here
  console.log(data, arguments) // `undefined` , `[]`
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>

解决方案

There are multiple possible strategies depending upon the specifics of what you're trying to do: Here's one option:

someArray.reduce(function(p, item) {
  return p.then(function(array) {
    return someFunction(item).then(function(val) {
        array.push(val);
        return array;
    });
  });
}, $.Deferred().resolve([])).then(function(array) {
  // all done here
  // accumulated results in array
}, function(err) {
  // err is the error from the rejected promise that stopped the chain of execution
});

Working demo: http://jsfiddle.net/jfriend00/d4q1aaa0/


FYI, the Bluebird Promise library (which is what I generally use) has .mapSeries() which is built for this pattern:

var someArray = [1,2,3,4];

Promise.mapSeries(someArray, function(item) {
    return someFunction(item);
}).then(function(results) {
    log(results);
});

Working demo: http://jsfiddle.net/jfriend00/7fm3wv7j/

这篇关于如何以下Array.prototype.reduce累积回报承诺返回值作为数组。那么()()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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