【发布时间】:2016-03-30 23:16:53
【问题描述】:
我正在尝试在 Q.allSettled 完成后解决延迟的承诺。但是,allSettled 的 .then 永远不会执行,并且永远不会返回 promise 数组结果。没有抛出异常我只是永远不会进入.then 块。正如您从下面的代码中看到的那样,我在 .then 块内迭代一些文件上传元素,然后根据这些返回执行文件上传和一些保存。
var deferred = Q.defer();
datacontext.getFileNameGuid(fileNamesNeeded).then(function (data) {
_.each($fileUploads, function(item){
if (item.files.length > 0) {
var promise = uploadFile(item, data[remainingFilesToUpload])
.then(function () {
return datacontext.saveItem(atom)
.then(function (data) {
return getItemSuccess(data, false);
}).done();
}).catch(function (data) {
logger.logError("An error occurred while updating your item. Please try again.", data, 'Error', true);
}).done();
fileUploadPromises.push(promise);
return promise;
}
});
// Either execute promises or bail
if (fileUploadPromises.length > 0) {
// Handle blockUI and spinner in last then.
Q.allSettled(fileUploadPromises).then(function () {
deferred.resolve('Images have been saved and scheduled for thumbnail generation.');
});
} else {
// Other logic here
}
});
return deferred.promise;
【问题讨论】:
-
首先,避免deferred antipattern!
-
在众多
then回调中,您从未涉足过哪些? -
这些
.done()电话没有任何理由 -
在 Q.allSettled 之后我需要执行
then... 我更新了上面的代码块。它有一个done -
那么,你确定 a)
fileUploadPromises.length真的是> 0b) 所有这些承诺最终都会兑现吗?
标签: javascript promise q