简体   繁体   English

async.waterfall无法正常工作

[英]async.waterfall not working as expected

I'm using async.waterfall for the first time and I'm running into some trouble. 我第一次使用async.waterfall ,遇到了一些麻烦。

Here are the two functions I'm trying to call: 这是我要调用的两个函数:

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

and

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

My waterfall function looks like this: 我的瀑布函数如下所示:

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

    });

I want to pass in data from the outer scope into generateImageURL and then pass that data on towards generateCoordinates along with the url . 我想将data从外部范围传递到generateImageURL ,然后将该数据与url传递给generateCoordinates The callback from generateCoordinates should call the anonymous function. generateCoordinatescallback应调用匿名函数。

My issue is that generateCoordinates is never being called. 我的问题是generateCoordinates从未被调用。 Even though I call it within generateImageURL . 即使我在generateImageURL调用它。

From the docs : 文档

Each function is passed a callback(err, result1, result2, ...) it must call on completion. 每个函数都传递了一个回调(err,result1,result2等),该回调必须在完成时调用。 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. 第一个参数是错误(可以为null),任何其他参数将作为参数传递给下一个任务。

and

If any of the tasks pass an error to their own callback, the next function is not executed. 如果有任何任务将错误传递给自己的回调,则不会执行下一个函数。

Your callback is callback(data, JSON.parse(xhr.responseText).data.link); 您的回调是callback(data, JSON.parse(xhr.responseText).data.link); , you pass data as the err argument, therefore it's interpreted as an error and your next function is not called. ,您将data作为err参数传递,因此将其解释为错误,并且不会调用您的下一个函数。 You should see it if you console.log(err) in your final callback. 如果在最终回调中使用console.log(err) ,则应该看到它。

Call your callback as callback(null, data, JSON.parse(xhr.responseText).data.link); 将您的回调称为callback(null, data, JSON.parse(xhr.responseText).data.link);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM