async.each里面没有触发async.waterfall回调

时间:2018-01-26 03:04:05

标签: javascript node.js asynchronous callback jasmine-node

我试图实现以下目标:

请考虑在不支持async / await的jasmine测试框架中运行

async.waterfall调用一个具有async.each的函数来触发模式和表的创建。异步瀑布中的步骤必须按顺序执行,即必须在创建表之前创建模式。我面临的问题是第一次调用创建模式,但回调永远不会返回到async.waterfall。因此,async.waterfall的下一步永远不会执行。

时间表或流程:
driverFunction (async.waterfall)调用 createFunction
createFunction (asyncCreateSchema等)函数为数组中的每个文件调用 doSomething
doSomething 执行jar文件并返回成功或错误。

这是我的代码:



'use strict'
let async = require('async');


function doSomething(file, done) {
  console.log(file);
  return done(null, true);
}

function asyncCreateSchema(files, done) {
  async.each(
    files,
    function(file, callback) {
      if (file.startsWith('schema')) {
        doSomething(file, callback);
      }
      else{
        callback();
      }
    },
    function(err) {
      if (err) {
        console.log(err);
      }
      console.log('create schema done');
    });
}

function asyncCreateTables(files, done) {
  async.each(
    files,
    function(file, callback) {
      if (file.startsWith('table')) {
        doSomething(file, callback);
      }
      else{
        callback();
      }
    },
    function(err) {
      if (err) {
        console.log(err);
      }
      console.log('create schema done');
    });
}

var files = ['schema.json', 'schema_1.json', 'table.json'];

async.waterfall([
    next => asyncCreateSchema(files, next),
    (nil, next) => asyncCreateTables(files, next),

  ],
  function(err, res) {
    if (err) {
      throw new Error("Setup error: " + err.message);
    } else {
      console.log(res);
    }
  }
);




我在这里做错了什么?请使用async npm包解释此场景中回调函数的流程。

1 个答案:

答案 0 :(得分:1)

  

为什么使用async.wattherfall函数代替ES2017 async/await

请向我展示更多代码,一些异步代码,不仅仅是console.log(),看看我如何能够取消那个丑陋的nodejs异步库,并用纯async/await语法替换。

我刚给你做了一些代码,但我对你想要的东西视而不见,请把你的代码的时间线也放在一边,例如,先将fn放在首位,然后将结果用于另一个。

async function doSomething(file) {
    var result = await new Promise((done, error) => {
        console.log(file)
        /* Here you execute async or sync operatios,
           when done, fire the done() if there is an error, you fire the error(error) */
        someTask(file, function(err, data){
            if (err)
                error(err)
            else
                done(data) //This data will be in result
        })
    })
    return result //data passed to done()
}

async function asyncCreateSchema(files, done) {
    for (var file of files) {
        if (file.startsWith('schema'))
            await doSomething(file);
    }
}

async function asyncCreateTables(files) {
    for (var file of files) {
        if (file.startsWith('table'))
            await doSomething(file);
    }
}


async function noNeedOfWaterfall () {
    var files = ['schema.json', 'schema_1.json', 'table.json']
    await asyncCreateSchema(files)
    await asyncCreateTables(files)
}

noNeedOfWaterfall()
    .then(() => console.log('all done'))
    .catch(e => {
        console.log('If you fire the error(err) in doSomething >>', e)
    })
相关问题