rsm*_*rts 5 asynchronous waterfall node.js
我明白了:
错误:类型错误:回调不是函数
代码:
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);
});
Run Code Online (Sandbox Code Playgroud)
为什么它会是一个错误?尽管“回调”是 async.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);
});
Run Code Online (Sandbox Code Playgroud)
来自文档:
无论我们在哪里接受 Node 风格的异步函数,我们也直接接受 ES2017 异步函数。在这种情况下,异步函数将不会被传递最终回调参数,并且任何抛出的错误将被用作隐式回调的 err 参数,并且返回值将被用作结果值。(即,返回的 Promise 被拒绝成为 err 回调参数,解析值成为结果。)
| 归档时间: |
|
| 查看次数: |
2223 次 |
| 最近记录: |