gpt4 book ai didi

javascript - async.waterfall 未按预期工作

转载 作者:行者123 更新时间:2023-12-02 14:25:23 25 4
gpt4 key购买 nike

我第一次使用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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com