I am new to both promises and having trouble understanding what I need to do to code the following logic:
我是两个承诺的新手,并且无法理解我需要做什么来编写以下逻辑:
I am developing a Web service in Node.js and Express to fetch song data from a wiki and return an object, which a client application will consume. The wiki's API does not let me write a batch query; I have to get each page individually. So I will have to get the list of songs, then perform a call for each song.
我正在Node.js和Express中开发一个Web服务来从wiki获取歌曲数据并返回一个客户端应用程序将使用的对象。维基的API不允许我编写批量查询;我必须单独获取每个页面。所以我必须得到歌曲列表,然后为每首歌曲打电话。
I currently intend to use the Q middleware for Node.js as my promises library, though I am open to suggestions on a more appropriate middleware for this task.
我目前打算将Node.js的Q中间件用作我的promises库,尽管我对这个任务的更合适的中间件提出了建议。
Here is my pseudocode:
这是我的伪代码:
app.get('/songs/:criteria', function(request,response) {
downloadSongList()
.then(foreach(song) downloadSongData)
.then(assembleReturnValue)
.then(response.json(returnValue));
});
What will the actual code look like?
实际代码会是什么样的?
2 个解决方案
#1
3
The actual code will use function expressions, and around the foreach
you will need to use Q.all
:
实际的代码将使用函数表达式,并且在foreach周围你需要使用Q.all:
app.get('/songs/:criteria', function(request,response) {
downloadSongList(request.params)
.then(function(list) {
var promises = list.map(function(song) {
return downloadSongData(song.params) // another promise
});
return Q.all(promises);
}).then(function(allResults) {
// assemble
return // Value;
}).then(response.json);
});
Also have a look at these general rules for promise development.
另请参阅这些承诺开发的一般规则。
#2
0
Here is an alternative solution with Bluebird, since you said you were interested in different libraries:
以下是Bluebird的替代解决方案,因为您说您对不同的库感兴趣:
downloadSongList(request.params).
map(downloadSongData).
call("join",",").
then(response.json).catch(sendError)
What we're utilizing here:
我们在这里使用的是:
-
.map
- which takes an array of promises, and calls a method on each one of them, we do that for the list returned fromdownloadSongList
. - .map - 它接受一个promises数组,并在每个promise上调用一个方法,我们为downloadSongList返回的列表执行此操作。
-
.call
which calls an array method, here we're joining the elements as strings, not sure what format you're using here but this will basically doarray.join
. - 调用数组方法的.call,这里我们将元素作为字符串加入,不知道你在这里使用的是什么格式,但这基本上就是array.join。
These are some advantages we gain from Bluebird, other than that this is very similar to Bergi's answer.
这些是我们从Bluebird获得的一些优势,除此之外,它与Bergi的答案非常相似。
#1
3
The actual code will use function expressions, and around the foreach
you will need to use Q.all
:
实际的代码将使用函数表达式,并且在foreach周围你需要使用Q.all:
app.get('/songs/:criteria', function(request,response) {
downloadSongList(request.params)
.then(function(list) {
var promises = list.map(function(song) {
return downloadSongData(song.params) // another promise
});
return Q.all(promises);
}).then(function(allResults) {
// assemble
return // Value;
}).then(response.json);
});
Also have a look at these general rules for promise development.
另请参阅这些承诺开发的一般规则。
#2
0
Here is an alternative solution with Bluebird, since you said you were interested in different libraries:
以下是Bluebird的替代解决方案,因为您说您对不同的库感兴趣:
downloadSongList(request.params).
map(downloadSongData).
call("join",",").
then(response.json).catch(sendError)
What we're utilizing here:
我们在这里使用的是:
-
.map
- which takes an array of promises, and calls a method on each one of them, we do that for the list returned fromdownloadSongList
. - .map - 它接受一个promises数组,并在每个promise上调用一个方法,我们为downloadSongList返回的列表执行此操作。
-
.call
which calls an array method, here we're joining the elements as strings, not sure what format you're using here but this will basically doarray.join
. - 调用数组方法的.call,这里我们将元素作为字符串加入,不知道你在这里使用的是什么格式,但这基本上就是array.join。
These are some advantages we gain from Bluebird, other than that this is very similar to Bergi's answer.
这些是我们从Bluebird获得的一些优势,除此之外,它与Bergi的答案非常相似。