处理返回承诺中的例外
我知道这是不是最漂亮的代码,但由于历史遗留问题,我要坚持这个工作流程。
问题是,我不能泡了可能发生在返回的承诺的心脏任何异常。
该代码的设计,无论是reject
和resolve
返回有效的数据。所以,如果你改变了const CONDITION
到0.4
,我们会得到拒绝。如果const CONDITION
值停留在0.6
,我们将得到一个解决方案。这工作至今。
然而,在我们有结构性故障,如在下面,我们尝试错误的变量名传递到拒绝的情况为例:
let reasoxxxx = '__FAILED__';
reject({error: reason, data: output});
我不能够调用throw
冒泡的错误。出于这个原因,我越来越平常丑消息,我不能泡了一个适当的异常:
Exchange request was rejected due to error(s).
(node:224) UnhandledPromiseRejectionWarning: ReferenceError: reason is not defined
(node:224) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:224) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
有任何想法吗?代码段应该工作。
function fakeFetch() {
// Promisify the request.
return new Promise((resolve, reject) => {
// Emulate an asynchroneous fetch.
setTimeout(() => {
let result = 0.4; // Change to 0.4 to trigger a failed fetch.
if (result < 0.5) {;
reject('__FAIL__');
} else {
resolve({name: 'apple', price: '1234.12', time: 1549926859970});
}
}, 2000);
});
}
async function sendExchangeRequest(id, pair, symbols, callback)
{
let err, result
await fakeFetch().then((output) => { result = output }).catch((error) => {err = error})
if(err){
result = 'None'
}else{
err = 'None'
}
callback(err, result)
}
async function fetchExchangeData(id, pair, symbols) {
// Promisify the request.
try {
return new Promise((resolve, reject) => {
try {
// Send the request.
sendExchangeRequest(id, pair, symbols, ((err, output) => {
try{
if(err){
// Soft Failure
console.log('Exchange request was rejected due to error(s).');
reject({error: err, data: output});
}else{
// Success
console.log('Exchange request was successful.');
resolve({error: err, data: output});
}
} catch(error) {
throw error;
}
}));
} catch(error) {
console.log('---\n', error, '\n---');
throw error;
}
});
} catch(error) {
// Bubble up the error?
console.log('+++\n', error, '\n+++');
throw error;
}
}
(async () => {
await fetchExchangeData('myid', 'MYPAIR', 'mySymbol')
.then((result) => console.log(result))
.catch((failure) => console.log(failure))
})();
回答如下:
我觉得你不理解的异步函数的概念。在try/catch
你的第一个fetchExchangeData
做本质上不过,并没有什么异步它。在最初的承诺在try / catch语句,也几乎是无用的,因为我相信大多数/与sendExchangeRequest
所有问题将有可能与错误处理功能(err)
处理。而对于try / catch块,你不应该抛出错误的诺言内,则应该拒绝错误(将泡起来)。只有try / catch语句,如果你在一个异步函数的时候,并没有使用回调。
function fetchExchangeData(id, pair, symbols) {
// Promisify the request.
return new Promise((resolve, reject) => {
try {
// Send the request.
sendExchangeRequest(id, pair, symbols, ((err, output) => {
try{
if(err){
// Soft Failure
console.log('Exchange request was rejected due to error(s).');
let reason = '__FAILED__';
reject({error: reason, data: output});
}else{
// Success
console.log('Exchange request was successful.');
resolve({error: '__NONE__', data: output});
}
} catch(error) {
reject(error);
}
}));
} catch(error) {
console.log('---\n', error, '\n---');
reject(error);
}
});
}
与本文相关的文章
评论列表(0)
- 暂无评论