¿Qué versión de async estás usando?
2.5.0
¿En qué entorno ocurrió el problema (versión de nodo / versión del navegador)?
Nodo 8
¿Qué hiciste?
async=require('async')
async function myFirstFunction(callback) {
callback(null, 'one', 'two');
}
function mySecondFunction(arg1, arg2, callback) {
// arg1 now equals 'one' and arg2 now equals 'two'
callback(null, 'three');
}
async function myLastFunction(arg1, callback) {
// arg1 now equals 'three'
callback(null, 'done');
}
async.waterfall([
myFirstFunction,
mySecondFunction,
myLastFunction,
], function (err, result) {
// result now equals 'done'
console.log(err, result)
});
Qué esperabas que sucediera?
no tener una excepción
¿Cuál fue el resultado real?
> TypeError: callback is not a function
at myFirstFunction (repl:2:5)
at /Users/scott/node_modules/async/dist/async.js:143:27
at /Users/scott/node_modules/async/dist/async.js:21:12
at nextTask (/Users/scott/node_modules/async/dist/async.js:5297:14)
at Object.waterfall (/Users/scott/node_modules/async/dist/async.js:5307:5)
at repl:1:7
at ContextifyScript.Script.runInThisContext (vm.js:44:33)
at REPLServer.defaultEval (repl.js:239:29)
at bound (domain.js:301:14)
at REPLServer.runBound [as eval] (domain.js:314:12) undefined
Las devoluciones de llamada no se pasan a las funciones async
, sino que simplemente devuelven un valor.
En el caso de la primera función anterior, donde se pasa más de un argumento a callback
en myFirstFunction
, ¿deberíamos devolver una matriz en su lugar?
Sí, podrías hacer algo como:
async.waterfall([
// ...
async function (arg1, arg2) {
//...
const arg3 = await foo()
return [arg1, arg2, arg3]
},
function ([arg1, arg2, arg3], callback) {
//...
}
Entonces, ¿cómo devolver el error? ¿Solo usas lanzar?
¿Alguna respuesta a la pregunta anterior? Creo que para rescatar cuando se encuentra un error en una función asíncrona, aún necesita llamar a la devolución de llamada "siguiente"
¿Qué tal esto?
async.waterfall([
// ...
async function (arg1, arg2, callback) {
//...
try {
const arg3 = await foo()
return [arg1, arg2, arg3]
} catch (err) {
callback('An error occured:' + err.message);
}
},
function ([arg1, arg2, arg3], callback) {
//...
}
async
funciones throw
un error.
Gracias.
Comentario más útil
Sí, podrías hacer algo como: