async.waterfall无法按预期工作

时间:2016-07-11 07:10:11

标签: javascript node.js

我第一次使用async.waterfall而我遇到了麻烦。

以下是我试图致电的两个功能:

function generateImageURL(data, callback){
   // ... xhr stuff
    xhr.onload = function () {
        callback(data, JSON.parse(xhr.responseText).data.link);
    }
  // ... more xhr stuff
    xhr.send(fd);
}

function generateCoordinates(data, url, callback){
    console.log("CALLED"); // never gets called
    navigator.geolocation.getCurrentPosition(function(p){
        data.image_url = url;
        data.coordinates = [p.coordinates.longitude, p.coordinates.latitude];
        callback(data);
    });
}

我的瀑布功能如下:

 async.waterfall([
        generateImageURL.bind(this, data),
        generateCoordinates
    ], function(err, result){

    });

我想将data从外部范围传递到generateImageURL,然后将该数据与generateCoordinates一起传递给url。来自callback的{​​{1}}应该调用匿名函数。

我的问题是永远不会调用generateCoordinates。即使我在generateCoordinates内调用它。

1 个答案:

答案 0 :(得分:2)

来自docs

  

每个函数都传递一个必须在完成时调用的回调(错误,结果1,结果2,...)。第一个参数是一个错误(可以为null),任何进一步的参数将作为参数传递,以便进行下一个任务。

  

如果任何任务将错误传递给他们自己的回调,则不执行下一个功能。

您的回调是err,您将console.log(err)作为callback(null, data, JSON.parse(xhr.responseText).data.link);参数传递,因此它被解释为错误而您的下一个函数未被调用。如果你在最后的回调中var Args = "/UseVsixExtensions:true" + " " + "\"" + @"D:\path\myDllTestNunit.dll" + "\"" + " " + "/TestAdapterPath:" + "\"" + @"C:\path\NUnit3TestAdapter.3.0.10\lib" + "\"" + " " + "/Logger:trx" + " /settings:" + "\"" + @"D:\pathRunsettings\dbci_2016_06_23_10_01_56.runsettings" + "\""; var cmdPath = @"C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe"; var proc = new Process(); proc.StartInfo.FileName = cmdPath; proc.StartInfo.Arguments = Args; proc.StartInfo.RedirectStandardOutput = true; proc.StartInfo.RedirectStandardError = true; proc.EnableRaisingEvents = true; proc.StartInfo.CreateNoWindow = false; proc.ErrorDataReceived += proc_DataReceived; proc.OutputDataReceived += proc_DataReceived; proc.StartInfo.UseShellExecute = false; proc.Start(); proc.BeginErrorReadLine(); proc.BeginOutputReadLine(); proc.WaitForExit(); Console.ReadLine(); ,你应该看到它。

将您的回调称为>>> Starting test execution, please wait... >>> Information: NUnit Adapter 3.0.10.0: Test execution started >>> >>> Information: Running all tests in D:\appli\statro\RSS3_BATCHES_TEST\UT\LANCE MENT_TESTS\RSS3.Batches.Test.Nunit.Tests.dll >>> >>> Warning: Dependent Assembly nunit.framework of D:\appli\statro\RSS3_BATCHES_ TEST\UT\LANCEMENT_TESTS\RSS3.Batches.Test.Nunit.Tests.dll not found. Can be igno red if not a NUnit project. >>> >>> Information: NUnit Adapter 3.0.10.0: Test execution complete >>> >>> Warning: No test is available in D:\appli\statro\RSS3_BATCHES_TEST\UT\LANCEM ENT_TESTS\RSS3.Batches.Test.Nunit.Tests.dll. Make sure that installed test disco verers & executors, platform & framework version settings are appropriate and tr y again.