我第一次使用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
网址generateCooperatives
的callback
应调用匿名函数。
我的问题是 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/