【问题标题】:Q.allSettled not returning promise resultsQ.allSettled 不返回承诺结果
【发布时间】: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 真的是 > 0 b) 所有这些承诺最终都会兑现吗?

标签: javascript promise q


【解决方案1】:

在听取@Bergi 的建议后,我重构了一堆使用deferred antipattern 的代码。我最终将我的代码包装在 Q.fcall 块中以返回承诺并将 Q.allSettled 更改为 Q.all。以下是我的工作代码的最终状态。

注意:我省略了对 Durandal 特定功能和其他功能的函数调用。

return Q.fcall(function () {
    try {

        var fileUploadPromises = [];
        var $fileUploads = $(fileUploadClass);
        var fileNamesNeeded = 0;

        $fileUploads.each(function (index, item) {
            if (item.files.length > 0)
                fileNamesNeeded++;
        });

        return datacontext.getFileNameGuid(fileNamesNeeded).then(function (data) {
            _.each($fileUploads, function (item) {
                if (item.files.length > 0) {
                    // Init upload file promise                                
                    var promise = uploadFile(item, data[remainingFilesToUpload])
                        .then(function () {
                            return datacontext.saveItem(atom)
                                .then(function (data) {
                                    return getItemSuccess(data, false);
                                });
                        }).catch(function (data) {                                        
                            logger.logError('An error occurred while updating your Item. Please try again.', data, 'Error', true);
                            app.trigger(config.publishedMessageNames.AtomFileUploaded, "");
                        });

                    fileUploadPromises.push(promise);

                    remainingFilesToUpload++;
                }
            });

            // Either execute promises or bail
            if (fileUploadPromises.length > 0) {                            
                return Q.all(fileUploadPromises)
                .then(function (results) {                                
                    return datacontext.scheduleBatchTask(filesToUpload(), item.DocumentId()).then(function () {
                        app.trigger(config.publishedMessageNames.FileUploaded, data);
                    });
                }).done();
            } else {
                app.trigger(config.publishedMessageNames.FileUploaded, "");
            }
        });                   
    } catch (e) {
        return new Error(e);
    }
}); 

【讨论】:

    猜你喜欢
    • 2016-10-22
    • 2017-02-03
    • 2019-10-20
    • 1970-01-01
    • 2019-02-06
    • 1970-01-01
    • 1970-01-01
    • 2017-09-10
    • 2016-04-30
    相关资源
    最近更新 更多