【问题标题】:Can I turn an array into an array of Promises?我可以将一个数组变成一个 Promise 数组吗?
【发布时间】:2015-04-18 21:32:01
【问题描述】:

我正在使用bluebird,如果这很重要的话。我有一个对象数组:

a = [obj1, obj2, obj3]

我想使用 Promise 对它们中的每一个进行迭代,然后进行一些处理。

a.each (obj) ->
  doStuff obj
.then (objArray) ->
  moreStuff()

我该怎么做?

更新

var addedFilesTree;

addedFilesTree = [1, 2, 3];

Promise.map(addedFilesTree).then(function(addedFileTree) {
  return console.log(addedFileTree);
});

是我的实际代码。这会返回一个错误:

Possibly unhandled TypeError: fn must be a function

    See http://goo.gl/916lJJ

【问题讨论】:

  • 是的,这是 CoffeeScript
  • 您的实际代码出错了,因为它缺少doStuff 函数。

标签: javascript arrays coffeescript promise bluebird


【解决方案1】:
Promise.map a, (obj) ->
  doStuff obj
.then (objArray) ->
  moreStuff()

【讨论】:

  • .map 会同时做事吗?
  • 这会返回一个错误:Possibly unhandled TypeError: fn must be a function See http://goo.gl/916lJJ
  • @Shamoon 签名是Promise.map a, (obj) ->,你甚至没有传递任何函数。
  • 哎呀!谢谢。 #DumbMistake
猜你喜欢
  • 2020-06-24
  • 2011-03-19
  • 1970-01-01
  • 1970-01-01
  • 2015-02-08
  • 2021-06-24
  • 1970-01-01
  • 2020-07-29
  • 2013-07-26
相关资源
最近更新 更多