javascript - async.waterfall 未按预期工作

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

我的 waterfall 函数如下所示:

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

    });

我想将data从外部范围传递到generateImageURL,然后将该数据与一起传递到generateCooperatives网址。来自 generateCooperativescallback 应调用匿名函数。

我的问题是 generateCooperatives 从未被调用。即使我在 generateImageURL 中调用它。

最佳答案

来自docs :

Each function is passed a callback(err, result1, result2, ...) it must call on completion. The first argument is an error (which can be null) and any further arguments will be passed as arguments in order to the next task.

If any of the tasks pass an error to their own callback, the next function is not executed.

您的回调是callback(data, JSON.parse(xhr.responseText).data.link);,您将data作为err传递code> 参数,因此它被解释为错误并且不会调用您的下一个函数。如果您在最终回调中 console.log(err) ,您应该会看到它。

将回调调用为 callback(null, data, JSON.parse(xhr.responseText).data.link);

关于javascript - async.waterfall 未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38301053/

相关文章:

javascript - 在大型 TypeScript 项目中共享类型的最佳方式?

javascript - 如何从 NodeJS 中的存储压缩文件 PDF

node.js - 处理 SailsJS 中的服务器错误

javascript - 内联条件

node.js - 使用 TS 将 process.env 转换为 <any>

javascript - 检测调查猴子 iframe 的变化

javascript - React-router 重定向到主页

javascript - 如何仅使用 JavaScript 登录 Twitter

javascript - 视网膜 Macbook Pro 的 WebGL 性能问题

javascript - 使用 Webpack library 和 libraryTarget 配置通过全局变量设置我们的自定义库