繁体   English   中英

在async.auto链中使用async / await会导致TypeError:回调不是函数

[英]Using async / await inside async.auto chain leads to TypeError: callback is not a function

您正在使用哪个版本的异步?

2.6.1

问题发生在哪个环境中(节点版本/浏览器版本)

8.11.3

你做了什么? 请提供一个可重现的最小案例来说明此问题。

假设fileObj是从外部提供的:

async.auto({
  download: (downloadCB) => {
    if (fileObj) {
      fs.writeFile(__dirname + ‘fileNew.txt’, fileObj.content, 'base64', function (err) {
        if (err){
          return downloadCB(err);
        }
        return downloadCB(null , fileObj.generatedFileName); // works fine
      });
    } else {
      let err = new Error('File not found');
      return downloadCB(err);
    }
  },
  collectData: ['download', async (results, collectCB) => {
    console.log(typeof collectCB); // prints undefined
    console.log(typeof results); // prints correct object

    let res = await anHttpRequest();
    if (res.response && res.response.statusCode == 200) {
      return collectCB(null , 'fileCombined.txt'); // This is where the ISSUE happens
    }
    else if(res.response.statusCode >= 300) {
      return collectCB(new Error('Request failed inside async-auto'));
    }
  }],

  filterData: ['collectData', (results, filterCB) => {
    doFilter(results.collectData, filterCB);
  }],
})

您期望发生什么?

collectData完成执行后,filterData应该开始执行在collectCB函数内部传递的参数

实际结果是什么?

TypeError:collectCB不是函数。

相同的代码在2.0.1版中可以很好地执行,但是在升级到2.6.1之后,它已经停止工作,这对我们来说至关重要。 任何解决方法也将不胜感激。

基于文档 (已在其他答案中引用,但此处再次引用)

无论我们在何处接受Node风格的异步函数,我们都直接接受ES2017异步函数。 在这种情况下,异步函数将不会传递给最终的回调参数,并且任何引发的错误都将用作隐式回调的err参数,而返回值将用作结果值。 (即拒绝返回的Promise成为err回调参数,而解析的值成为结果。)

你会做的是

async.auto({
    download: (downloadCB) => {
        if (fileObj) {
            fs.writeFile(__dirname + ‘fileNew.txt’, fileObj.content, 'base64', function(err) {
                if (err) {
                    return downloadCB(err);
                }
                return downloadCB(null, fileObj.generatedFileName); // works fine
            });
        } else {
            let err = new Error('File not found');
            return downloadCB(err);
        }
    },
    //                                      Note, no callback as per documentation
    collectData: ['download', async (results) => {
        console.log(typeof results); // prints correct object

        let res = await anHttpRequest();

        if (res.response && res.response.statusCode == 200) {
            // this return is equivalent to callback(null, value);
            return 'fileCombined.txt';
        } else if (res.response.statusCode >= 300) {
            // this throw is equivalent to callback(err);
            throw new Error('Request failed inside async-auto');
        }
        // but surely something should be here!? for status code 201-209?
    }],

    filterData: ['collectData', (results, filterCB) => {
        doFilter(results.collectData, filterCB);
    }],
})

只是从官方文档中复制粘贴:

无论我们在何处接受Node风格的异步函数,我们都直接接受ES2017异步函数。 在这种情况下, 不会向async函数传递最终的回调参数 ,并且任何引发的错误都将用作隐式回调的err参数,而返回值将用作结果值。 (即拒绝返回的Promise成为err回调参数,而解析的值成为结果。)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM