簡體   English   中英

Q承諾鏈錯誤后存在承諾鏈

[英]Q promise chain exists promise chain after error

我有一個node.js腳本,該腳本打開一個Azure容器,在將它們流式傳輸到Azure容器的同時拍攝多個國家/地區的頁面的屏幕截圖。 我遇到的問題是,如果我在流傳輸過程中遇到錯誤,它將完成給定ID的其余屏幕截圖,然后退出承諾鏈。

因此,如果我在ID 211006遇到錯誤,它將完成所有屏幕截圖的獲取,然后退出流。 它不會繼續下去。

我對211006工作方式以及它們如何捕獲錯誤是非常211006 ,但是我的理解是,例如211006確實遇到錯誤,腳本將完成211006鏈,然后在運行.fin之前向我顯示任何錯誤-事實並非如此。

有人可以幫忙嗎?

AzureService.createContainer()
    .then(function () {
        return ScreenshotService.getAllCountriesOfId('308572');
    })
    .then(function () {
        return ScreenshotService.getAllCountriesOfId('211006');
    })
    .then(function () {
        return ScreenshotService.getAllCountriesOfId('131408');
    })
    .then(function () {
        return ScreenshotService.getAllCountriesOfId('131409');
    })
    .then(function () {
        return ScreenshotService.getAllCountriesOfId('789927');
    })
    .then(function () {
        return ScreenshotService.getAllCountriesOfId('211007');
    })
    .then(function () {
        return ScreenshotService.getAllCountriesOfId('833116');
    })

    // Upload Log file into Azure storage
    .fin(function () {
        AzureService.init({
            container: config.azure.storage.msft.CONTAINER.LOG,
            account: config.azure.storage.msft.ACCOUNT,
            key: config.azure.storage.msft.ACCESS_KEY,
            file: config.file.log,
            isLogFile: true
        });

        log.info('Utility: Uploading log file [ %s ] to Azure storage container [ %s ]', AzureService.file, AzureService.container);

        return AzureService.uploadLocalFileToStorage()
            .then(function () {
                return util.deleteFile({fileName: AzureService.file, isLogFile: true});
            })
            .fail(function (err) {
                log.info(err);
            })
            .done();
    })

    .fail(function (err) {
        log.info(err);
    })

    .done();

只要允許錯誤返回鏈中,諾言鏈就會停止。 這會將promise狀態設置為拒絕,並將在任何后續的.then()處理程序中調用下一個錯誤處理程序,而不是完成的處理程序。

如果希望鏈繼續,則需要捕獲錯誤。 捕獲錯誤將使Promise基礎結構將其視為“已處理”,並且Promise狀態將再次被實現,並且它將繼續執行已實現的處理程序。

承諾錯誤類似於異常。 如果未處理它們,它們將中止處理,直到第一個異常處理程序為止。 如果使用異常處理程序處理它們,則處理將在該異常處理程序之后正常繼續。

在特定情況下,如果您希望繼續進行跟蹤,則需要處理以下每種類型的行中的錯誤:

return ScreenshotService.getAllCountriesOfId('308572');

您可以這樣做:

return ScreenshotService.getAllCountriesOfId('308572').then(null, function(err) {
    console.log(err);
    // error is now handled and processing will continue
});

由於您有很多重復的代碼,因此您可能應該將代碼更改為可通過一系列國家/地區ID進行迭代的內容,而不是一遍又一遍地復制代碼行。


這是一種使用.reduce()在循環中鏈接所有promise的方法,並消除了太多重復的代碼並處理各個國家/地區的錯誤,因此鏈接繼續進行:

var countryIds = ['308572', '211006', '131408', '131409', '789927', '211007', '833116'];
countryIds.reduce(function(p, item) {
    return p.then(function() {
        return ScreenshotService.getAllCountriesOfId(item).then(null, function(err) {
            console.log(err);
        });
    });
}, AzureService.createContainer())
// Upload Log file into Azure storage
.fin(function () {
   ... rest of your code continued here

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM