Javascript 如何使用bluebird将原始数据与Promise.map的响应一起传递?
我有一些名为Javascript 如何使用bluebird将原始数据与Promise.map的响应一起传递?,javascript,coffeescript,promise,bluebird,Javascript,Coffeescript,Promise,Bluebird,我有一些名为photos的数组,它以承诺的形式返回: somePromiseFunc.then (resp) -> photos = _.filter resp, 'invalid' photos .map (photo) -> request url: photo.url method: 'GET' .each (photo_contents) -> # HERE I NEED THE ORIGINAL ph
photos
的数组,它以承诺的形式返回:
somePromiseFunc.then (resp) ->
photos = _.filter resp, 'invalid'
photos
.map (photo) ->
request
url: photo.url
method: 'GET'
.each (photo_contents) ->
# HERE I NEED THE ORIGINAL photo and the photo_contents
如何在响应中同时获得
照片
和照片内容
?这是可能的吗?最简单的方法是在映射中组合它们
回调:
somePromiseFunc().then (resp) ->
_.filter resp, 'invalid'
.map (photo) ->
request
url: photo.url
method: 'GET'
.then (photo_content) ->
[photo, photo_content]
.each ([photo, content]) ->
# …
当然,对于元组,也可以使用对象而不是数组
另一种方法是将阵列连接在一起:
photos = somePromiseFunc().then (resp) ->
_.filter resp, 'invalid'
contents = photos.map (photo) ->
request
url: photo.url
method: 'GET'
Promise.all [photos, contents]
.then ([photos, contents]) ->
Promise.each (_.zip photos, contents), ([photo, content]) ->
# …
最简单的方法是在
map
回调中组合它们:
somePromiseFunc().then (resp) ->
_.filter resp, 'invalid'
.map (photo) ->
request
url: photo.url
method: 'GET'
.then (photo_content) ->
[photo, photo_content]
.each ([photo, content]) ->
# …
当然,对于元组,也可以使用对象而不是数组
另一种方法是将阵列连接在一起:
photos = somePromiseFunc().then (resp) ->
_.filter resp, 'invalid'
contents = photos.map (photo) ->
request
url: photo.url
method: 'GET'
Promise.all [photos, contents]
.then ([photos, contents]) ->
Promise.each (_.zip photos, contents), ([photo, content]) ->
# …
您可以使用
承诺。所有:
somePromiseFunc.then (resp) ->
photos = _.filter resp, 'invalid'
photos
.map (photo) ->
Promise.all [
photo
request
url: photo.url
method: 'GET'
]
.each ([photo, contents]) ->
由于您使用的是bluebird,如果您更喜欢在对象而不是数组中传递值,也可以使用Promise.props
,但在这种特殊情况下真正做的就是添加一些额外的详细信息:
somePromiseFunc.then (resp) ->
photos = _.filter resp, 'invalid'
photos
.map (photo) ->
Promise.props
photo: photo
contents: request
url: photo.url
method: 'GET'
.each ({photo, contents}) ->
您可以使用承诺。所有:
somePromiseFunc.then (resp) ->
photos = _.filter resp, 'invalid'
photos
.map (photo) ->
Promise.all [
photo
request
url: photo.url
method: 'GET'
]
.each ([photo, contents]) ->
由于您使用的是bluebird,如果您更喜欢在对象而不是数组中传递值,也可以使用Promise.props
,但在这种特殊情况下真正做的就是添加一些额外的详细信息:
somePromiseFunc.then (resp) ->
photos = _.filter resp, 'invalid'
photos
.map (photo) ->
Promise.props
photo: photo
contents: request
url: photo.url
method: 'GET'
.each ({photo, contents}) ->
这一连串的承诺似乎。。。少于desirable@Shamoon当前位置你并不是真正的嵌套承诺,而是将它们嵌套在一个循环中。参见JLRishe的答案,了解一种非常类似的方法,即使用承诺。所有而不是内部。然后(+1对他来说)这种承诺的嵌套似乎。。。少于desirable@Shamoon当前位置你并不是真正的嵌套承诺,而是将它们嵌套在一个循环中。请参阅JLRishe的答案,了解一种非常类似的方法,即使用Promise.all
而不是内部。然后(+1对他来说)你也可以做{photo,contents:…}
来减少冗长,尽管我想知道括号(和额外的行!)或者复制更详细:-@Bergi第二种方法使用的非空白字符比第一种方法多13个。Coffeescript用户讨厌非空白字符。;-)不过,说真的,仅仅为了拥有属性名而添加属性名让我觉得很笨拙,尤其是在使用了很好的数组解构语法的情况下。如果OP将映射的结果传递到某个地方,而不是立即传递到每个,我会说第二种方法是有好处的。你也可以做{photo,contents:…}
,以减少冗长,尽管我想知道括号(和额外的行!)或者复制更详细:-@Bergi第二种方法使用的非空白字符比第一种方法多13个。Coffeescript用户讨厌非空白字符。;-)不过,说真的,仅仅为了拥有属性名而添加属性名让我觉得很笨拙,尤其是在使用了很好的数组解构语法的情况下。如果OP将映射的结果传递到某个地方,而不是立即传递到每个,我会说第二种方法有优点。