如何串行链接http请求函数
How to chain http request functions in serially
我需要按顺序依次从三个 http 请求中获取响应。
我能够使用嵌套函数来做到这一点。我还需要使用全局范围内最后一个请求的响应,这是我无法使用嵌套解决方案实现的。
var request = require("request");
httpRequest1((getRequest1) => {
console.log(getRequest1);
httpRequest2((getRequest2) => {
console.log(getRequest2);
httpRequest3((getRequest3) => {
console.log(getRequest3);
});
});
});
function httpRequest1 (callback){
var options = { method: 'POST',
url: };
request(options, function (error, response, body) {
if (error) throw new Error(error);
callback (body);
});}
function httpRequest2(callback){
var options = { method: 'POST',
url: };
request(options, function (error, response, body) {
if (error) throw new Error(error);
callback(body);
});}
function httpRequest3(callback){
var options = { method: 'POST',
url: };
request(options, function (error, response, body) {
if (error) throw new Error(error);
callback(body);
});}
您应该考虑更现代的 async/await
模式并使用它来解决承诺。它将使您的代码对您自己和其他人更具可读性。
阅读此博客了解更多详情,特别是您案例中的 "Chaining Operations" 部分:https://www.sitepoint.com/simplifying-asynchronous-coding-async-functions/
function httpRequest1 (callback){
var options = { method: 'POST', url: };
return new Promise(function (resolve, reject) {
request(options, function (error, response, body) {
if (error) return reject(error);
resolve(body);
});
});
}
function httpRequest2 (callback){
var options = { method: 'POST', url: };
return new Promise(function (resolve, reject) {
request(options, function (error, response, body) {
if (error) return reject(error);
resolve(body);
});
});
}
function httpRequest3 (callback){
var options = { method: 'POST', url: };
return new Promise(function (resolve, reject) {
request(options, function (error, response, body) {
if (error) return reject(error);
resolve(body);
});
});
}
承诺
httpRequest1()
.then(body1 =>
return httpRequest2();
)
.then(body2 =>
return httpRequest3();
)
.then(body3 =>
).catch(error => {
// error code
} );
Async/Await
function async getResponse() {
try {
const body1 = await httpRequest1();
const body2 = await httpRequest2();
const body3 = await httpRequest3();
} catch (e) {
console.log(e);
}
}
已更新
Await 在正常功能下不起作用。如果使用 await,则必须在 asyn 函数中使用它。如果你想从普通函数调用序列化异步函数,那么 promise 是更好的选择。但你可以使用另一种方式。假设我们有一个像上面那样的异步函数,其中 return of response
function async getResponse() {
try {
const body1 = await httpRequest1();
const body2 = await httpRequest2();
const body3 = await httpRequest3();
return { body1, body2, body3 }
} catch (e) {
console.log(e);
}
}
function normalFunction() {
// const res = getResponse(); it will not work
getResponse()
.then(result => {
// you will get the result here
})
}
后台异步函数 return promise Result 。这就是为什么我们可以这样写。
var request = require("request-promise");
( async () => {
const getRequest1 = await httpRequest1();
const getRequest2 = await httpRequest2();
const getRequest3 = await httpRequest3();
})();
async function httpRequest1() {
var options = {
method: 'POST',
url: ''
};
const body = await request(options)
if (!body) throw "error";
return body;
}
async function httpRequest2() {
var options = {
method: 'POST',
url: ''
};
const body = await request(options)
if (!body) throw "error";
return body;
}
async function httpRequest3() {
var options = {
method: 'POST',
url: ''
};
const body = await request(options)
if (!body) throw "error";
return body;
}
如果请求是独立的,可以使用Promise.all()。在调用httpRequest2等之前,您不必等待httpRequest1的响应。
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all
我需要按顺序依次从三个 http 请求中获取响应。
我能够使用嵌套函数来做到这一点。我还需要使用全局范围内最后一个请求的响应,这是我无法使用嵌套解决方案实现的。
var request = require("request");
httpRequest1((getRequest1) => {
console.log(getRequest1);
httpRequest2((getRequest2) => {
console.log(getRequest2);
httpRequest3((getRequest3) => {
console.log(getRequest3);
});
});
});
function httpRequest1 (callback){
var options = { method: 'POST',
url: };
request(options, function (error, response, body) {
if (error) throw new Error(error);
callback (body);
});}
function httpRequest2(callback){
var options = { method: 'POST',
url: };
request(options, function (error, response, body) {
if (error) throw new Error(error);
callback(body);
});}
function httpRequest3(callback){
var options = { method: 'POST',
url: };
request(options, function (error, response, body) {
if (error) throw new Error(error);
callback(body);
});}
您应该考虑更现代的 async/await
模式并使用它来解决承诺。它将使您的代码对您自己和其他人更具可读性。
阅读此博客了解更多详情,特别是您案例中的 "Chaining Operations" 部分:https://www.sitepoint.com/simplifying-asynchronous-coding-async-functions/
function httpRequest1 (callback){
var options = { method: 'POST', url: };
return new Promise(function (resolve, reject) {
request(options, function (error, response, body) {
if (error) return reject(error);
resolve(body);
});
});
}
function httpRequest2 (callback){
var options = { method: 'POST', url: };
return new Promise(function (resolve, reject) {
request(options, function (error, response, body) {
if (error) return reject(error);
resolve(body);
});
});
}
function httpRequest3 (callback){
var options = { method: 'POST', url: };
return new Promise(function (resolve, reject) {
request(options, function (error, response, body) {
if (error) return reject(error);
resolve(body);
});
});
}
承诺
httpRequest1()
.then(body1 =>
return httpRequest2();
)
.then(body2 =>
return httpRequest3();
)
.then(body3 =>
).catch(error => {
// error code
} );
Async/Await
function async getResponse() {
try {
const body1 = await httpRequest1();
const body2 = await httpRequest2();
const body3 = await httpRequest3();
} catch (e) {
console.log(e);
}
}
已更新
Await 在正常功能下不起作用。如果使用 await,则必须在 asyn 函数中使用它。如果你想从普通函数调用序列化异步函数,那么 promise 是更好的选择。但你可以使用另一种方式。假设我们有一个像上面那样的异步函数,其中 return of response
function async getResponse() {
try {
const body1 = await httpRequest1();
const body2 = await httpRequest2();
const body3 = await httpRequest3();
return { body1, body2, body3 }
} catch (e) {
console.log(e);
}
}
function normalFunction() {
// const res = getResponse(); it will not work
getResponse()
.then(result => {
// you will get the result here
})
}
后台异步函数 return promise Result 。这就是为什么我们可以这样写。
var request = require("request-promise");
( async () => {
const getRequest1 = await httpRequest1();
const getRequest2 = await httpRequest2();
const getRequest3 = await httpRequest3();
})();
async function httpRequest1() {
var options = {
method: 'POST',
url: ''
};
const body = await request(options)
if (!body) throw "error";
return body;
}
async function httpRequest2() {
var options = {
method: 'POST',
url: ''
};
const body = await request(options)
if (!body) throw "error";
return body;
}
async function httpRequest3() {
var options = {
method: 'POST',
url: ''
};
const body = await request(options)
if (!body) throw "error";
return body;
}
如果请求是独立的,可以使用Promise.all()。在调用httpRequest2等之前,您不必等待httpRequest1的响应。
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all