【问题标题】:Nodejs request post multiple sets of data?Nodejs请求发布多组数据?
【发布时间】:2018-08-04 23:02:17
【问题描述】:

我有很多请求要连续发送到同一个 URL,但每次我都必须传递不同的参数。我想知道是否有任何方法可以一次完成论文?这是请求选项对象中的当前数据

form: {
    encrValue: encrValue
}

或者一种知道它们何时全部完成的方法,这是一个比多个请求部分更大的问题

【问题讨论】:

    标签: node.js httprequest


    【解决方案1】:

    您只能使用 API 接受的内容,如果它不接受多个请求,则您不能这样做。 将它们一起发送的一个好方法是使用Promise.all

    例如:

    const arrOfPromises = [];
    for(const item of items) {
        arrOfPromises.push(fetch('getSomethingForThisItem', { body: JSON.stringify(item) }));
    }
    
    Promise.all(arrOfPromises)
    .then(res => console.log('this is the array of responses:', res))
    .catch(err => console.error('Oops, something went wrong!', err));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-14
      • 2014-08-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-26
      • 2020-09-24
      相关资源
      最近更新 更多