【发布时间】:2018-10-13 12:37:13
【问题描述】:
我想要实现的是同步执行一个有序的HTTP请求序列,但是根据一些变量的值,我希望其中一些被绕过。
作为一个例子(我在 Javascript 中使用 request 库来做到这一点):
request(httpReq1, function(error, response, body) {
// do something, like handling errors...
request(httpReq2, function(error, response, body) {
// do something else
});
});
这样可以确保httpReq2 将在httpReq1 之后执行。
我不确定如何绕过第一个请求,例如,如果某些标志设置为 false,而不是执行 httpReq1 并等待响应,它只是跳转到 httpReq2, 保持秩序:
if (dontMakeHttpReq1) // where should this be placed?
request(httpReq1, function(error, response, body) {
// do something, like handling errors...
request(httpReq2, function(error, response, body) {
// do something else
});
});
什么是解决这个问题的好方法?
【问题讨论】:
标签: javascript node.js json http request