<p>async 関数を指定すると async.waterfall が壊れる (ノード 8)</p>

作成日 2017年10月05日  ·  8コメント  ·  ソース: caolan/async

使用している非同期のバージョンは何ですか?
2.5.0

問題が発生した環境 (ノードのバージョン/ブラウザのバージョン)
ノード8

あなたは何をした?

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

何が起こると予想していましたか?
例外がないように

実際の結果はどうでしたか?

> 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

最も参考になるコメント

ええ、あなたは次のようなことができます:

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

全てのコメント8件

コールバックはasync関数に渡されず、単に値を返します。

上記の最初の関数の場合、複数の引数がmyFirstFunction callbackに渡されますが、代わりに配列を返す必要がありますか?

ええ、あなたは次のようなことができます:

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

では、どのようにエラーを返すのですか? 投げるだけ?

上記の質問への回答はありますか? 非同期関数でエラーが発生した場合は解決すると思いますが、「次の」コールバックを呼び出す必要があります

これはどう?

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関数は、渡されたコールバックを受け取りません。 ちょうどthrowエラーです。

ありがとう。

このページは役に立ちましたか?
0 / 5 - 0 評価