Promises - 错误回调与捕获
Promises - error callback vs. catch
有人可以告诉我在使用 $q.promise
时使用错误回调与 catch
函数之间是否有区别吗?
例如这两段代码在功能上是否相同?
function doSomething0() {
var deferred = $q.defer();
...
return deferred.promise;
}
doSomething0()
.then(doSomething1)
.then(doSomething2)
.then(doSomething3)
.catch(function (err) {
// do something with `err`
});
对比
function doSomething0() {
var deferred = $q.defer();
...
return deferred.promise;
}
function errorHandler(err) {
// do something with `err`
}
doSomething0()
.then(doSomething1, errorHandler)
.then(doSomething2, errorHandler)
.then(doSomething3, errorHandler);
如果是这样,为什么要使用第二个?在我看来,它看起来更丑陋并且会导致更多的代码重复?
两者都将实现相同的目的,除了第二个可能 运行 errorHandler
三次(而不是一次)。你是对的,它带来了一些代码重复,但它也允许你处理发生的任何错误并继续你的链:
function errorHandler(err) {
//log error, continue
return $q.resolve('default value or something');
}
doSomething0()
.then(doSomething1, errorHandler)
.then(doSomething2, errorHandler)
.then(doSomething3, errorHandler);
让我们看一下第一个示例:
doSomething0()
.then(doSomething1, errorHandler)
.then(doSomething2, errorHandler)
.then(doSomething3, errorHandler);
// I've represented functions invocations as if they were synchronous to simplify the example to focus on the error handling
// The above sample is in a way "equivalent" to
try {
// doSomething0()
try {
// doSomething1()
try {
// doSomething2()
} catch(e0) {
// handle error
}
} catch(e1) {
// handle error
}
} catch(e2) {
// handle error
}
// doSomething3()
但是如果doSomething3
处理程序发生异常,则不会处理。
好的,我们来看第二个示例:
doSomething0()
.then(doSomething1)
.then(doSomething2)
.then(doSomething3)
.catch(function (err) {
// do something with `err`
});
// I've represented functions invocations as if they were synchronous to simplify the example to focus on the error handling
// The above sample is in a way "equivalent" to
try {
// doSomething0()
// doSomething1()
// doSomething2()
// doSomething3()
}
catch(e) {
// Catch 'em all
console.log(e)
}
有人可以告诉我在使用 $q.promise
时使用错误回调与 catch
函数之间是否有区别吗?
例如这两段代码在功能上是否相同?
function doSomething0() {
var deferred = $q.defer();
...
return deferred.promise;
}
doSomething0()
.then(doSomething1)
.then(doSomething2)
.then(doSomething3)
.catch(function (err) {
// do something with `err`
});
对比
function doSomething0() {
var deferred = $q.defer();
...
return deferred.promise;
}
function errorHandler(err) {
// do something with `err`
}
doSomething0()
.then(doSomething1, errorHandler)
.then(doSomething2, errorHandler)
.then(doSomething3, errorHandler);
如果是这样,为什么要使用第二个?在我看来,它看起来更丑陋并且会导致更多的代码重复?
两者都将实现相同的目的,除了第二个可能 运行 errorHandler
三次(而不是一次)。你是对的,它带来了一些代码重复,但它也允许你处理发生的任何错误并继续你的链:
function errorHandler(err) {
//log error, continue
return $q.resolve('default value or something');
}
doSomething0()
.then(doSomething1, errorHandler)
.then(doSomething2, errorHandler)
.then(doSomething3, errorHandler);
让我们看一下第一个示例:
doSomething0()
.then(doSomething1, errorHandler)
.then(doSomething2, errorHandler)
.then(doSomething3, errorHandler);
// I've represented functions invocations as if they were synchronous to simplify the example to focus on the error handling
// The above sample is in a way "equivalent" to
try {
// doSomething0()
try {
// doSomething1()
try {
// doSomething2()
} catch(e0) {
// handle error
}
} catch(e1) {
// handle error
}
} catch(e2) {
// handle error
}
// doSomething3()
但是如果doSomething3
处理程序发生异常,则不会处理。
好的,我们来看第二个示例:
doSomething0()
.then(doSomething1)
.then(doSomething2)
.then(doSomething3)
.catch(function (err) {
// do something with `err`
});
// I've represented functions invocations as if they were synchronous to simplify the example to focus on the error handling
// The above sample is in a way "equivalent" to
try {
// doSomething0()
// doSomething1()
// doSomething2()
// doSomething3()
}
catch(e) {
// Catch 'em all
console.log(e)
}