【问题标题】:Batch Processing in Node.js POST RequestNode.js POST 请求中的批处理
【发布时间】:2020-11-13 06:03:39
【问题描述】:

我写信通知我有一个请求,我需要在 40 个项目中发送几个请求项目,并且下一批必须在 10 秒休息后处理。我已经更改了参数并假设如果我运行,

const request = require('request'); let options = {   'method': 'POST',   'url': 'https://somewebsite.com/uk/v1/process/day_assignments.json',   'headers': {
    'Content-Type': 'application/json',
    'Accept': 'application/json',
    'Authorization': 'Bearer *'   },   body: JSON.stringify([{"officer_id":6789,"major_id":[345]}, {"officer_id":1357,"major_id":[678]}, {"officer_id":9135,"major_id":[901]}, {"officer_id":2468,"major_id":[234]},])

}; request(options, (error, response) => {   if (error) throw new Error(error);   console.log(response.body);

然后在 10 秒后它应该处理:

const request = require('request'); let options = {   'method': 'POST',   'url': 'https://somewebsite.com/uk/v1/process/day_assignments.json',   'headers': {
    'Content-Type': 'application/json',
    'Accept': 'application/json',
    'Authorization': 'Bearer *'   },   body: JSON.stringify([{"officer_id":0123,"major_id":[123]}, {"officer_id":4567,"major_id":[456]}, {"officer_id":8901,"major_id":[789]}, {"officer_id":2345,"major_id":[012]}])

}; request(options, (error, response) => {   if (error) throw new Error(error);   console.log(response.body);

我是 node.js 的新手,我该如何处理。任何帮助将不胜感激。

【问题讨论】:

  • 尝试使用 setInterval

标签: node.js async-await request settimeout


【解决方案1】:

关注这个
调用第一个请求
在第一个请求回调中使用 settimeout 10 秒。
在 settimeout 回调中调用第二个请求。

【讨论】:

  • 我要处理 100 多个批次,有什么最佳解决方案吗?
  • 编写一个 for 循环并等待 10 秒,然后再处理 [batch jobs] 中的下一批 for i 进程 i 作业等待 10 秒。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-10
  • 2023-02-09
相关资源
最近更新 更多