原生es用法
function getItems(param) {
try {
var items = getItemsSync();
if(!items) throw new InvalidItemsError();
} catch(e) {
// can address the error here, either from getItemsSync returning a falsey value or throwing itself
throw e; // need to re-throw the error unless I want it to be considered handled.
}
return process(items);
}
在bluebird更加简单(Similarly, with promises)
function getItems(param) {
return getItemsAsync().then(items => {
if(!items) throw new InvalidItemsError();
return items;
}).catch(e => {
// can address the error here and recover from it, from getItemsAsync rejects or returns a falsey value
throw e; // Need to rethrow unless we actually recovered, just like in the synchronous version
}).then(process);
}
捕获到的错误会propagate最近的catch
.catch(function(any error) handler) -> Promise
.caught(function(any error) handler) -> Promise
This is a catch-all exception handler, shortcut for calling .then(null, handler) on this promise. Any exception happening in a .then-chain will propagate to nearest .catch handler.
For compatibility with earlier ECMAScript versions, an alias .caught is provided for .catch.(早期版本中caught是catch的alias)
Filtered Catch(可以自定义过滤器)
.catch(
class ErrorClass|function(any error)|Object predicate...,
function(any error) handler
) -> Promise
.caught(
class ErrorClass|function(any error)|Object predicate...,
function(any error) handler
) -> Promise
This is an extension to .catch to work more like catch-clauses in languages like Java or C#. Instead of manually checking instanceof or .name === "SomeError", you may specify a number of error constructors which are eligible for this catch handler. The catch handler that is first met that has eligible constructors specified, is the one that will be called.(不用像java或者c#那样判断)
例
“`
somePromise.then(function() {
return a.b.c.d();
}).catch(TypeError, function(e) {
//If it is a TypeError, will end up here because
//it is a type error to reference property of undefined
}).catch(ReferenceError, function(e) {
//Will end up here if a was never declared at all
}).catch(function(e) {
//Generic catch-the rest, error wasn’t TypeError nor
//ReferenceError
});
““
还可以一次写多个过滤条件(You may also add multiple filters for a catch handler)
somePromise.then(function() {
return a.b.c.d();
}).catch(TypeError, ReferenceError, function(e) {
//Will end up here on programmer error
}).catch(NetworkError, TimeoutError, function(e) {
//Will end up here on expected everyday network errors
}).catch(function(e) {
//Catch any unexpected errors
});
自定以err类型需要将prototype指向Error的prototype(Such a constructor can be minimally created like so)
function MyCustomError() {}
MyCustomError.prototype = Object.create(Error.prototype);
Using it:
Promise.resolve().then(function() {
throw new MyCustomError();
}).catch(MyCustomError, function(e) {
//will end up here now
});
也可以在catch的处理函数例resolve(By not returning a rejected value or throwing from a catch, you “recover from failure” and continue the chain)
Promise.reject(Error('fail!'))
.catch(function(e) {
// fallback with "recover from failure"
return Promise.resolve('success!'); // promise or value
})
.then(function(result) {
console.log(result); // will print "success!"
});
This is exactly like the synchronous code:
var result;
try {
throw Error('fail');
} catch(e) {
result = 'success!';
}
console.log(result);
millions译