Javascript 使用async.js顺序执行多个HTTP请求
如何使用async.js按顺序执行多个HTTP请求。我查看了async.js文档,但不知道该怎么做。我希望使用async.js回调样式实现与下面代码相同的功能Javascript 使用async.js顺序执行多个HTTP请求,javascript,node.js,callback,async.js,Javascript,Node.js,Callback,Async.js,如何使用async.js按顺序执行多个HTTP请求。我查看了async.js文档,但不知道该怎么做。我希望使用async.js回调样式实现与下面代码相同的功能 var http = require('http'); var Q = require('q'); var URL="http://localhost:3000"; var getPromise=function(url) { var deferred = Q.defer(); var req = http.get(url
var http = require('http');
var Q = require('q');
var URL="http://localhost:3000";
var getPromise=function(url) {
var deferred = Q.defer();
var req = http.get(url, function(response) {
if(response.statusCode < 200 || response.statusCode > 299){
deferred.reject(new Error('ErrorCode '+response.statusCode))
}
var result="";
response.on('data',function(chunk){result +=chunk;} )
response.on('end',function(){deferred.resolve(result);} )
});
req.on('error',function(err){
console.error('Error with the request:', err.message);
deferred.reject(err);
});
req.end();
return deferred.promise;
}
getPromise('http://localhost:3000/olympic/2016/ranking/4')
.then(function(data){
console.log("Response 1 "+data)
return getPromise(URL+'/iso/country/'+JSON.parse(data).Country);
})
.then(function(data){
console.log("Response 2 "+data)
return getPromise(URL+'/olympic/2016/medal/'+JSON.parse(data).iso);
})
.then(function(data){
console.log("Response 3 "+data)
})
.catch(function(err){
console.log(err)
});
var http=require('http');
var Q=要求('Q');
变量URL=”http://localhost:3000";
var getPromise=函数(url){
var deferred=Q.deferred();
var req=http.get(url,函数(响应){
if(response.statusCode<200 | | response.statusCode>299){
延迟.拒绝(新错误('ErrorCode'+response.statusCode))
}
var结果=”;
on('data',函数(chunk){result+=chunk;})
on('end',function(){deferred.resolve(result);})
});
请求开启('error',函数(err){
console.error('error with the request:',err.message);
延迟。拒绝(错误);
});
请求结束();
回报。承诺;
}
getPromise('http://localhost:3000/olympic/2016/ranking/4')
.then(功能(数据){
控制台日志(“响应1”+数据)
返回getPromise(URL+'/iso/country/'+JSON.parse(data.country);
})
.then(功能(数据){
控制台日志(“响应2”+数据)
返回getPromise(URL+'/olympic/2016/medal/'+JSON.parse(data.iso);
})
.then(功能(数据){
控制台日志(“响应3”+数据)
})
.catch(函数(err){
console.log(错误)
});
仔细查看一下代码,并试图进一步理解它,我相信这就是您需要的功能。这将按顺序运行每个函数,并将其结果传递给序列中的下一个函数。下面是一个例子:
async.waterfall([
function(callback)
{
// Function 1: do request here...
callback(null, val); // replace null with a value if you want the waterfall to error and go straight to the end
},
function(val, callback) {
// Function 2: do your second request here
callback(null, val1, val2, val3); // you can pass any number of variables you like, just make sure the next function in the sequence expects them
},
function(val1, val2, val3, callback)
{
// Function 3: do your third request here
callback(null, result);
} // this can go on for as long as you like
], function(err, result)
{
// this will be called immediately if the first parameter in any of the callbacks is not null, or when all the functions have run
});
我得到了它,我需要async.瀑布,它需要一个函数数组并逐个执行它们。我们还可以将前一个函数执行的结果传递给下一个
var async = require('async');
async.waterfall([
function task1(done) {
console.log('start!');
setTimeout(function(){
console.log("T1 Complete");
// <- set value to passed to step 2
done(null, 'Value from step 1');
},5000);
},
function task2(task1Result, done) {
console.log(task1Result);
setTimeout(function(){
console.log("T2 Complete");
// <- set value to passed to step 3
done(null, 'Value from step 2');
},1000);
},
function task3 (task2Result, done) {
console.log(task2Result);
setTimeout(function(){
console.log("T3 Complete");
// <- no value set for the next step.
done(null);
},100);
}
],
function (err) {
if (err) {
throw new Error(err);
} else {
console.log('No error happened in any steps, operation done!');
}
});
var async=require('async');
异步瀑布([
功能任务1(完成){
console.log('start!');
setTimeout(函数(){
控制台日志(“T1完成”);
//顺序和Acsi.js似乎是彼此完全相反的。与当前的答案相反,您似乎想要使用当前调用中的前一个调用的响应。考虑使用<代码>异步。SEQ < /代码>,我没有使用承诺,所以不是100%肯定它们是如何工作的,而是每个<代码>。然后< /代码>使用返回的值。以前的承诺,还是它们都是单独的调用?@ScottMarcus我们不应该有时需要以前HTTP请求的结果来完成并传递响应以进行另一个HTTP调用。我不认为异步和顺序执行是完全相反的事情。您所描述的是一个同步过程,它与异步相反是的,有时候我们确实希望这样,但JavaScript默认就是这样运行的。标准的AJAX调用和JavaScript承诺就是为了实现您想要的。