Javascript 承诺链中的async.queue?
我试图为api的get请求数组创建一个异步队列,只是不确定如何组合和使用响应。也许我的实现是错误的,因为我在promise then函数中使用async.queue 最终,我希望从first promise获得结果-> 使用第一个承诺的结果为async.queue创建get请求数组-> 然后合并所有get响应的结果。由于API速率限制,我需要限制每次发出的请求量Javascript 承诺链中的async.queue?,javascript,node.js,express,asynchronous,request,Javascript,Node.js,Express,Asynchronous,Request,我试图为api的get请求数组创建一个异步队列,只是不确定如何组合和使用响应。也许我的实现是错误的,因为我在promise then函数中使用async.queue 最终,我希望从first promise获得结果-> 使用第一个承诺的结果为async.queue创建get请求数组-> 然后合并所有get响应的结果。由于API速率限制,我需要限制每次发出的请求量 const rp = require("request-promise"); app.get("/", (req,res) =>
const rp = require("request-promise");
app.get("/", (req,res) => {
let arr = []
rp.get(url)
.then((response) => {
let arrayID = response
let q = async.queue((task, callback) => {
request({
method: "GET",
url: url,
qs: {
id: task.id
}
}, (error, response, body) => {
arr.push(body)
console.log(arr.length)
// successfully gives me the response i want. im trying to push into an array with all of my responses,
// but when i go to next then chain it is gone or if i try to return arr i get an empty []
})
callback()
}, 3)
for(var i = 0; i < arrayID.length; i++){
q.push({ id : arrayID[i]} );
}
q.drain = function() {
console.log('all items have been processed');
}
return arr
})
.then((responseArray) => {
//empty array even though the length inside the queue said other wise, i know its a problem with async and sync actions but is there a way to make the promise chain and async queue play nice?
res.json(responseArray)
})
})
const rp=require(“请求承诺”);
应用程序获取(“/”,(请求,请求)=>{
设arr=[]
rp.get(url)
。然后((响应)=>{
让arrayID=响应
让q=async.queue((任务,回调)=>{
请求({
方法:“获取”,
url:url,
qs:{
id:task.id
}
},(错误、响应、正文)=>{
前推(车身)
控制台日志(arr.length)
//成功地给了我想要的响应。我正在尝试将所有响应放入一个数组中,
//但当我转到下一个,然后链就消失了,或者如果我试图返回arr,我会得到一个空的[]
})
回调函数()
}, 3)
对于(变量i=0;i{
//空数组即使队列中的长度表示其他方面,我知道异步和同步操作存在问题,但有没有办法让承诺链和异步队列发挥作用?
res.json(responseArray)
})
})
- 要并行启动多个异步调用,可以使用
Promise.all()
- 要按顺序启动多个异步调用(即它们相互依赖),可以返回每个承诺并在
函数中使用其结果then()
解决了这个问题,最后不得不用一个承诺来包装它,并在q.drain()中解析最终的数组
嘿,谢谢,但最初我尝试使用promise.all时,我从api iwas usini获得了速率限制,所以我需要async.queue之类的东西,或者我可以用promise.all来模拟它吗?我需要能够在一个承诺中做10个,然后再做10个,依此类推
app.get("/", (req,res)
.then(function(firstResult)) {
//You can use result of first promise here
return Promise.all([
//Create array of get request here
//To also return firstResult just add it in the Promise.All array
]);
})
.then(function(allResults){
//You can use results of all the get requests created in the previous then()
})
.catch(function(error){
//Deal with any error that happened
});
const rp = require("request-promise");
app.get("/", (req,res) => {
rp.get(url)
.then((response) => {
let arrayID = response
return new Promise((resolve, reject) => {
var q = async.queue((task, callback) => {
request({
method: "GET",
url: url,
qs: {
id:task.id,
},
}, (error, response, body) => {
arr.push(body)
callback();
})
}, 2);
q.drain = () => resolve(arr);
q.push(arrayID);
})
})
.then((response) => res.json(response))
.catch((error) => res.json(error))
}