【问题标题】:Execute HTTP requests in by-pass order以绕过顺序执行 HTTP 请求
【发布时间】: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


    【解决方案1】:

    在一个数组中整理出所需的请求列表,并使用async/await依次执行它们

    let requests = [];
    if (doRequest1)
      requests.push(httpReq1);
    
    if (doRequest2)
      requests.push(httpReq2);
    
    /* etc .. for httpReq3 and so on */
    
    // now execute them one by one in sequence
    for(let req of requests) {
       try {
         await request(req);
       } catch (err) {
         // error handling here
       }
    }
    

    【讨论】:

    • 当然,这是要走的路!
    【解决方案2】:

    您可以使用 async-await 来实现。

    async apiCall(){ 
        try{
         if(condition){
          const result1 = await request(httpReq1);
         }
         const result2 = await request(httpReq2);
        }
        catch(error){
    
        }
    }
    

    确保请求模块重新运行承诺。否则创建一个承诺包装器。 axios 是一个基于 promise 的库。

    您必须将 async 放在包含等待的函数之前

    【讨论】:

    • 嘿,看起来不错。问题是我只想跳过第一个(理想情况下,主链中的任何请求)请求,具体取决于某个变量的状态:您总是发出第一个请求。
    • 检查编辑。只需将 await 调用视为同步语句。它们按顺序执行并等待回调被调用
    • 没问题。只是好奇。
    【解决方案3】:

    为什么不使用 2 个条件?

    if (dontMakeHttpReq1) {
      request(req1, function(error, response, body) {
        yourRequestProcessing2();
      });
    }
    else {
      request(req1, function(error, response, body) {
        request(req2, function(error, response, body) {
          yourRequestProcessing2();
        });
        yourRequestProcessing1();
      });
    }
    

    编辑:也许您想将请求调用存储在数组中

    //Whether request n should skip
    var flags = [
      false,
      true,
      false
    ];
    var requests = [
      function(error, response, body) {
        //Process your request 1
      },
      function(error, response, body) {
        //Process your request 2
      },
      function(error, response, body) {
        //Process request 3
      }
    ];
    for (i = 0; i < requests.length; i++) {
      if (flags[i]) {
        request(req1, requests[i + 1]);
      }
      else {
        request(req2, request[i]);
      }
    }
    

    【讨论】:

    • 您好,谢谢您的回答。我不喜欢这种方法,因为我有多个请求链。所以我尽量避免重复代码。
    • 请求都是基于之前的请求吗?没有从请求 3 到请求 2 的跳转?
    • 它们应该按顺序执行,并始终保持链中的请求数量。
    猜你喜欢
    • 2016-04-08
    • 2017-11-02
    • 1970-01-01
    • 2010-09-24
    • 1970-01-01
    • 1970-01-01
    • 2018-12-03
    • 1970-01-01
    • 2019-06-24
    相关资源
    最近更新 更多