JavaScript 中的函数结果为 return 后如何处理函数的意外错误?
How to handle an unexpected error from a function after result is return from function in JavaScript?
在 JavaScript 中处理异步函数时。我发现了这个问题。
函数需要执行一些异步任务,但是该任务对函数结果没有影响。所以函数 return 它的结果,一段时间后从异步函数抛出异常,但是由于控制已经 returned 所以异常没有处理。
注意:不改notify
我是故意抛出这样的错误,只需要处理这个异常
代码如下:
function notify() { //To send notification
setTimeout( function() { //Just to simulate an asynchronous function
throw "Exception occurred"; //An exception from async function
},2000);
return "Notified";
}
try {
let result = notify();
console.log(result); //Notified
}
catch (error) { //Never comes to catch block.
console.log(error);
}
如何捕捉这个异常。
尝试使用 Promise,但是它没有通过 promise 解决,因为即使使用 promise,流程或函数也会从内存中删除,因为调用函数已收到响应。
使用 Promises 的代码
function notify() { //To send notification
return new Promise ( (resolve, reject) => {
setTimeout( function() { //Just to simulate an asynchronous function
throw "Exception occurred"; //An exception from async function
},2000);
resolve("Notified");
});
}
notify()
.then( result => {
console.log(result); //Notified
})
.catch( error => { //Never comes to catch block
console.log(error);
});
如何在 JavaScript 编程中捕获异常?
我会将 setTimeout 包装在一个承诺中:
const delay = ms => new Promise(res => setTimeout(res, ms));
这允许您将 notify
写为:
async function notify() {
await delay(2000);
throw new Error();
}
notify().then(/*...*/).catch(/*...*/);
这里的技巧是 throw
在异步函数内部,而不是像你的情况那样在回调内部。
var notify = function () {
anOddTask();
return "Notified";
}
async function anOddTask() {
try {
await setTimeout( function() {
},2000);
throw new Error("Exception occurred");
}
catch (err) {
console.log(err);
}
}
try {
let result = notify();
console.log(result); //Notified
}
catch (error) { //Never comes to catch block.
console.log(error);
}
这将立即 return "Notify" 并在稍后处理拒绝。如果您希望 "Notify" 被 return 编辑为已解决的承诺,而不仅仅是将 notify()
设为异步函数。
var notify = async function () {
anOddTask();
return "Notified";
}
在 JavaScript 中处理异步函数时。我发现了这个问题。
函数需要执行一些异步任务,但是该任务对函数结果没有影响。所以函数 return 它的结果,一段时间后从异步函数抛出异常,但是由于控制已经 returned 所以异常没有处理。
注意:不改notify
我是故意抛出这样的错误,只需要处理这个异常
代码如下:
function notify() { //To send notification
setTimeout( function() { //Just to simulate an asynchronous function
throw "Exception occurred"; //An exception from async function
},2000);
return "Notified";
}
try {
let result = notify();
console.log(result); //Notified
}
catch (error) { //Never comes to catch block.
console.log(error);
}
如何捕捉这个异常。 尝试使用 Promise,但是它没有通过 promise 解决,因为即使使用 promise,流程或函数也会从内存中删除,因为调用函数已收到响应。
使用 Promises 的代码
function notify() { //To send notification
return new Promise ( (resolve, reject) => {
setTimeout( function() { //Just to simulate an asynchronous function
throw "Exception occurred"; //An exception from async function
},2000);
resolve("Notified");
});
}
notify()
.then( result => {
console.log(result); //Notified
})
.catch( error => { //Never comes to catch block
console.log(error);
});
如何在 JavaScript 编程中捕获异常?
我会将 setTimeout 包装在一个承诺中:
const delay = ms => new Promise(res => setTimeout(res, ms));
这允许您将 notify
写为:
async function notify() {
await delay(2000);
throw new Error();
}
notify().then(/*...*/).catch(/*...*/);
这里的技巧是 throw
在异步函数内部,而不是像你的情况那样在回调内部。
var notify = function () {
anOddTask();
return "Notified";
}
async function anOddTask() {
try {
await setTimeout( function() {
},2000);
throw new Error("Exception occurred");
}
catch (err) {
console.log(err);
}
}
try {
let result = notify();
console.log(result); //Notified
}
catch (error) { //Never comes to catch block.
console.log(error);
}
这将立即 return "Notify" 并在稍后处理拒绝。如果您希望 "Notify" 被 return 编辑为已解决的承诺,而不仅仅是将 notify()
设为异步函数。
var notify = async function () {
anOddTask();
return "Notified";
}