在一个项目中发现以下代码,看不懂:
I found the following code in a project, that I do not understand.:
get(key, store = null) {
if (!key) {
return new Error('There is no key to get!');
}
let dbstore = this.localforage;
if (store !== null) {
dbstore = store;
}
return dbstore
.getItem(key)
.then(function(value) {
return value;
})
.catch(function(err) {
return new Error('The key (' + key + ") isn't accessible: " + err);
});
}
为什么return new Error('There is no key to get!');
而不是throw new Error('There is no key to get!');
?
为什么不在 catch
块中抛出错误?
Also why not throw an error in the catch
block?
当你设计一个函数接口并且有错误需要处理时,你就有了如何返回错误的设计选择.如果函数是同步的,您可以返回一些指示错误并且很容易与实际结果区分开来的标记值(在 Javascript 中通常为 null
),或者您可以 throw
异常,或者您可以返回具有指示操作成功或失败的属性的对象.
When you design a function interface and there are errors to deal with, you have a design choice for how to return errors. If the function is synchronous, you can either return some sentinel value that indicates an error and is easily distinguished from an actual result (often null
in Javascript) or you can throw
an exception or you can return an object that has a property that indicates the success or failure of the operation.
当您使用 Promise 接口进行异步操作时,通常会使用 Error
对象拒绝 Promise
作为表示错误的拒绝原因.这就是 Promise 的核心设计理论.成功以可选值解决,错误以原因拒绝.
When you have an asynchronous operation with a promise interface, one would usually reject the Promise
with an Error
object as the reject reason to signify an error. That's the core design theory of promises. Success resolves with an optional value, errors reject with a reason.
这段代码:
return dbstore
.getItem(key)
.then(function(value) {
return value;
})
.catch(function(err) {
return new Error('The key (' + key + ") isn't accessible: " + err);
});
正在使用值或 Error
对象解析返回的承诺.这通常不是 promise 代码的编写方式,因为它需要调用者测试解析值的类型以确定是否存在错误,这不是使用 promise 的简单、直接的方法.所以,对于你的问题,你通常会这样做:
is resolving the returned promise with either a value or an Error
object. This is generally not how promise code is written because it will require the caller to test the type of the resolved value to figure out if there's an error or not which is not the simple, straightforward way to use promises. So, to your question, you would usually do this:
return dbstore.getItem(key).catch(function(err) {
throw new Error('The key (' + key + ") isn't accessible: " + err);
});
这个函数还有其他迹象,它只是糟糕的代码.
There are other signs in this function, that it's just bad code.
.then(function(value) {return value;})
完全是多余和不必要的.它根本没有增加任何价值.value
已经是 promise 的解析值.无需再次声明.
.then(function(value) {return value;})
is completely superfluous and unnecessary. It adds no value at all. The value
is already the resolved value of the promise. No need to declare it again.
该函数有时会返回一个承诺,有时会抛出一个同步异常.
这甚至是一个进一步的使用痛苦.如果您查看第一个 if (!key) {
语句,它会返回一个错误对象,即未提供 key
参数.这意味着要使用此函数,您必须捕获同步异常,提供 .then()
和 .catch()
处理程序并检查已解析承诺的类型以查看是否它恰好是一个错误对象.使用此功能是一场噩梦.这是糟糕的代码.
The function sometimes returns a promise and sometimes throws a synchronous exception.
This is even a further pain to use. If you look at the first if (!key) {
statement, it returns an Error object is the key
argument isn't supplied. That means that to use this function you have to catch synchronous exceptions, provide .then()
and .catch()
handlers AND check the type of the resolved promise to see if it happens to be an error object. This function is a nightmare to use. It's bad code.
要按原样使用函数,调用者可能必须这样做:
To use the function as it is, the caller would likely have to do this:
let retVal = someObj.get(aKey);
if (typeof retVal === Error) {
// got some synchronous error
} else {
retVal.then(val => {
if (typeof val === Error) {
// got some asynchronous error
} else {
// got an actual successful value here
}
}).catch(err => {
// got some asynchronous error
})
}
函数实现大概应该是这样的:
The function implementation probably should be this:
get(key, store = null) {
if (!key) {
return Promise.reject(new Error('There is no key to get!'));
}
let dbstore = store || this.localforage;
return dbstore.getItem(key).catch(function(err) {
throw new Error('The key (' + key + ") isn't accessible: " + err);
});
}
然后可以这样使用:
someObj.get(aKey).then(val => {
// got some successful value here
}).catch(err => {
// got some error here
});
将这里调用者的简单性与上面的混乱进行比较.
Compare the simplicity for the caller here to the mess above.
此实现具有以下一致性:
This implementation has these consistencies:
key
,它会返回一个被拒绝的 promise..then()
处理程序.key
isn't supplied, it returns a rejected promise..then()
handler that does nothing useful.这篇关于返回错误和抛出错误的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!