<p>async.waterfall bricht ab, wenn eine async-Funktion angegeben wird (Knoten 8)</p>

Erstellt am 5. Okt. 2017  ·  8Kommentare  ·  Quelle: caolan/async

Welche Version von Async verwendest du?
2.5.0

In welcher Umgebung trat das Problem auf (Knotenversion/Browserversion)
Knoten 8

Was hast du gemacht?

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)
});

Was haben Sie erwartet?
keine Ausnahme haben

Was war das tatsächliche Ergebnis?

> 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
docs question

Hilfreichster Kommentar

Ja, du könntest so etwas tun:

async.waterfall([
  // ...
  async function (arg1, arg2) {
    //...
    const arg3 = await foo()
    return [arg1, arg2, arg3]
  },
  function ([arg1, arg2, arg3], callback) {
    //...
  }

Alle 8 Kommentare

Callbacks werden nicht an async Funktionen übergeben, sondern geben einfach einen Wert zurück.

Sollten wir im Fall der ersten obigen Funktion, bei der mehr als ein Argument an callback in myFirstFunction , stattdessen ein Array zurückgeben?

Ja, du könntest so etwas tun:

async.waterfall([
  // ...
  async function (arg1, arg2) {
    //...
    const arg3 = await foo()
    return [arg1, arg2, arg3]
  },
  function ([arg1, arg2, arg3], callback) {
    //...
  }

Wie kann man dann den Fehler zurückgeben? Nur Wurf verwenden?

Irgendwelche Antworten auf die obige Frage? Ich glaube, um abzuspringen, wenn ein Fehler in einer asynchronen Funktion auftritt, müssen Sie immer noch den "nächsten" Rückruf aufrufen

Wie wäre es damit?

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 Funktionen erhalten keine Callbacks. Nur throw ein Fehler.

Vielen Dank.

War diese Seite hilfreich?
0 / 5 - 0 Bewertungen