5

I get this:

Error: TypeError: callback is not a function

Code:

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

why can it be an error? even though "callback" is the default function of async.waterfall. is it not possible in the async function I put the async function in it?

rsmnarts
  • 185
  • 2
  • 14

1 Answers1

4

When you use async in a function inside the waterfall, there's no callback argument. Instead of calling callback(null, data) you resolve 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);
});

From the 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.)

Marcos Casagrande
  • 37,983
  • 8
  • 84
  • 98