我正在开发一个客户端JS应用程序,该应用程序应读取CSV文件,每行进行几次API调用,然后将结果写回到CSV。我坚持的部分是如何协调请求并在所有步骤完成后启动功能。这是我到目前为止的内容:
var requests = [];
// loop through rows
addresses.forEach(function (address, i) {
// make request
var addressRequest = $.ajax({
dataType: 'json',
url: 'http://api.com/addresses/' + address,
success: function (data, textStatus, jqXhr) { APP.didGetAddressJson(data, i, jqXhr) },
error: function (jqXhr, textStatus, errorThrown) { APP.didFailToGetAddressJson(errorThrown, i) },
});
requests.push(addressRequest);
// make some more requests (handled by other success functions)
});
// leggo
$.when.apply($, requests).done(APP.didFinishGeocoding);
问题是,如果其中一行抛出404,则不会调用
done
函数。我将其切换为always
,现在它被调用了,但是没有在结尾处—如果我将每个回调的执行记录到控制台,通常在中间。但是,如果我编辑CSV以便没有错误,则它将按预期在最后被调用。我在这里做的事情允许always
提前触发吗?更新:仅仅是控制台将其记录为out of order吗?
最佳答案
您需要防止错误将$.when.apply($, requests)
返回的承诺发送到错误路径。
这可以通过以下方式实现:
将.then()
链接到您的$.ajax()
调用,而不是将“成功”和“错误”处理程序指定为$.ajax()
选项。
通过转换为成功来处理错误(因为这是jQuery,您必须从错误处理程序中返回已解决的Promise)。
这种方法还允许您控制最终传送到APP.didFinishGeocoding()
的数据
基于一些假设,您的代码的一般形状应如下所示:
function foo () {//assume there's an outer function wrapper
var errorMarker = '**error**';
var requests = addresses.map(function (address, i) {
return $.ajax({
dataType: 'json',
url: 'http://api.com/addresses/' + address
}).then(function (data, textStatus, jqXhr) { //success handler
return APP.didGetAddressJson(data, i, jqXhr); //whatever APP.didGetAddressJson() returns will appear as a result at the next stage.
}, function (jqXhr, textStatus, errorThrown) { // error handler
APP.didFailToGetAddressJson(errorThrown, i);
return $.when(errorMarker);//errorMarker will appear as a result at the next stage - but can be filtered out.
});
// make some more requests (handled by other success functions)
});
return $.when.apply($, requests).then(function() {
//first, convert arguments to an array and filter out the errors
var results = Array.prototype.slice.call(arguments).filter(function(r) {
return r !== errorMarker;
});
//then call APP.didFinishGeocoding() with the filtered results as individual arguments.
return APP.didFinishGeocoding.apply(APP, results);
//alternatively, call APP.didFinishGeocoding() with the filtered results as an array.
//return APP.didFinishGeocoding(results);
});
}
根据需要进行调整。
关于javascript - jQuery-.always()回调触发得太早,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30790604/