簡體   English   中英

藍鳥的承諾catch()不使用Promise.CancellationError調用內部函數

[英]bluebird promise catch() is not calling inner function with Promise.CancellationError

當文件丟失時,我試圖取消承諾。 但是,當我這樣做時,我會在輸出中看到:

Unhandled rejection Error: ENOENT, open '/home/one/github/infrastructure_manager_ui/gulp/util/token-file.json'
  at Error (native)

並且createTokenFile()無法正常運行。 不知道我在做什么錯:

 function refreshToken() {
        var tokenFile = path.join(__dirname, 'token-file.json');
        return tokenPromise = fs.readFileAsync(tokenFile, {encoding: 'utf-8'})
        .then(JSON.parse)
        .cancellable()
        .catch(Promise.CancellationError, function(err) {
            console.log(err);
            if (err.code !== 'ENOENT') { 
                throw err;
            } else {
                createTokenFile();
                tokenPromise.cancel();
            }
        });
}

.cancellable()在這里沒有做任何事情。 .cancellable()將一個承諾變成一個可以手動取消的承諾。 您在這里沒有做任何取消操作的事情,因此也沒有被取消。

如果要捕獲文件讀取錯誤,則應捕獲它:

function refreshToken() {
        var tokenFile = path.join(__dirname, 'token-file.json');
        return tokenPromise = fs.readFileAsync(tokenFile, {encoding: 'utf-8'})
        .then(JSON.parse)
        .catch(function(err) {
            console.log(err);
            if (err.code !== 'ENOENT') { 
                throw err;
            } else {
                return createTokenFile();
            }
        });
}

暫無
暫無

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

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