如何在蓝鸟中捕捉 ENOENT?
How to catch ENOENT in bluebird?
由于 fs.exists 弃用,我喜欢在 bluebird 中捕获 ENOENT。
例如:
.then(() => {
return promisedFs.unlinkAsync(excelPath);
})
.catch(ENOENT ERROR, () => { //do something })
.catch(all other errors, () => {//do something})
来自the docs:
A filtered variant (like other non-JS languages typically have) that
lets you only handle specific errors.
[…]
Predicate functions that only check properties have a handy shorthand.
In place of a predicate function, you can pass an object, and its
properties will be checked against the error object for a match:
fs.readFileAsync(...)
.then(...)
.catch({code: 'ENOENT'}, function(e) {
console.log("file not found: " + e.path);
});
The object predicate passed to .catch
in the above code
({code: 'ENOENT'}
) is shorthand for a predicate function
function predicate(e) { return isObject(e) && e.code == 'ENOENT' }
,
I.E. loose equality is used.
由于 fs.exists 弃用,我喜欢在 bluebird 中捕获 ENOENT。
例如:
.then(() => {
return promisedFs.unlinkAsync(excelPath);
})
.catch(ENOENT ERROR, () => { //do something })
.catch(all other errors, () => {//do something})
来自the docs:
A filtered variant (like other non-JS languages typically have) that lets you only handle specific errors.
[…]
Predicate functions that only check properties have a handy shorthand. In place of a predicate function, you can pass an object, and its properties will be checked against the error object for a match:
fs.readFileAsync(...) .then(...) .catch({code: 'ENOENT'}, function(e) { console.log("file not found: " + e.path); });
The object predicate passed to
.catch
in the above code ({code: 'ENOENT'}
) is shorthand for a predicate functionfunction predicate(e) { return isObject(e) && e.code == 'ENOENT' }
, I.E. loose equality is used.