【问题标题】:Async send parameters to methods异步向方法发送参数
【发布时间】:2021-01-28 02:06:51
【问题描述】:
router.post('/historical_data', (req, res, next) => {
  let ramdonName = Math.floor(Math.random() * Math.floor(6));
  callRequest(0, 1, 1, 2, true, ramdonName).then((data) => {
    if(!data.error){
        let totalRecords = data.totalRecords;
        let times = Math.ceil(totalRecords / 10000);
        async.timesSeries(times, getHistoricalData).then(endGetHistoricalData);
    }
  });

});

  function getHistoricalData(n, done) {
    // NEED ramdonName
  }
  function endGetHistoricalData(n, done) {
    // NEED ramdonName
  }

ramdonName 是一个动态变量,我尝试将变量 ramdonName 发送到方法 getHistoricalData,但 async.timesSeries 我不知道如何从其他方法发送

【问题讨论】:

  • 制作这两个高阶函数,然后将randomName 作为参数传递?将其添加到参数列表并部分应用功能?
  • 您好,谢谢,为什么我将变量 randomName 传递给方法 getHistoricalData
  • @VLAZ 如何添加到参数列表中应用?

标签: javascript node.js async-await async.js


【解决方案1】:

答案在这里 使用绑定() getHistoricalData.bind(null, ramdonName)

函数的第一个参数

function getHistoricalData(ramdonName, n, done) {
  console.log(ramdonName); // get the value 
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-18
    • 2022-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    • 2015-03-30
    相关资源
    最近更新 更多