简体   繁体   English

蓝鸟的承诺catch()不使用Promise.CancellationError调用内部函数

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

When a file is missing, I am trying to cancel the promise. 当文件丢失时,我试图取消承诺。 However, when I do this I see in the output: 但是,当我这样做时,我会在输出中看到:

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

and also createTokenFile() does not run as it should. 并且createTokenFile()无法正常运行。 Not sure what I am doing wrong: 不知道我在做什么错:

 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() is not doing anything here. .cancellable()在这里没有做任何事情。 .cancellable() turns a promise into one can be manually cancelled. .cancellable()将一个承诺变成一个可以手动取消的承诺。 You're not doing anything to cancel it here, so it's not being cancelled. 您在这里没有做任何取消操作的事情,因此也没有被取消。

If you want to catch the file read error, you should just catch it: 如果要捕获文件读取错误,则应捕获它:

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