gpt4 book ai didi

node.js - 我使用异步 waterfall ,为什么回调不是函数?

转载 作者:太空宇宙 更新时间:2023-11-03 22:20:52 25 4
gpt4 key购买 nike

我明白了:

Error: TypeError: callback is not a function

代码:

var async = require('async');

async.waterfall([
(callback) => {
callback(null, 'test');
},
async (value1, callback) => {
const data = await send("http://google.com/search?q="+value1);

callback(null, data); //TypeError: cb is not a function
}
], (err) => {
if (err) throw new Error(err);
});

为什么会出现错误?尽管“回调”是 async.waterfall 的默认功能。难道在异步函数中我把异步函数放进去是不可能的吗?

最佳答案

当您在 waterfall 内的函数中使用async时,没有callback参数。您无需调用 callback(null, data),而是解析 data

async.waterfall([
(callback) => {
callback(null, 'test');
},
async value1 => {
const data = await send("http://google.com");

return data;
},
(value1, callback) => {
// value1 is the resolve data from the async function
}
], (err) => {
if (err) throw new Error(err);
});

来自docs :

Wherever we accept a Node-style async function, we also directly accept an ES2017 async function. In this case, the async function will not be passed a final callback argument, and any thrown error will be used as the err argument of the implicit callback, and the return value will be used as the result value. (i.e. a rejected of the returned Promise becomes the err callback argument, and a resolved value becomes the result.)

关于node.js - 我使用异步 waterfall ,为什么回调不是函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55432421/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com