[英]Can I stop a function with the execution of a nested function?
I am trying to write a chrome extension and have the following code:我正在尝试编写 chrome 扩展并具有以下代码:
chrome.runtime.onMessage.addListener((req, sender, sendRes) => {
function cancel_error() {
sendRes({error:true});
return;
};
if (!req || !req.type)
cancel_error();
cancel_error();
console.log("continued");
sendRes({testRespone: true});
});
In this snippet the listener still logs continued and sends the test response.在此片段中,侦听器仍继续记录并发送测试响应。 is there a way to stop the function like calling return with the execution of a nested function?
有没有办法停止 function 就像调用 return 执行嵌套的 function 一样?
Just add return;
只需添加
return;
after you call the function:在您致电 function 后:
chrome.runtime.onMessage.addListener((req, sender, sendRes) => {
function cancel_error() {
sendRes({error:true});
};
if (!req || !req.type) {
cancel_error();
return;
}
console.log("continued");
sendRes({testRespone: true});
});
Adding return after you call the function will work调用 function 后添加 return 将起作用
if (!req || !req.type) {
cancel_error();
return;
}
or if you want to perform logic in your function, I would just return a value and then check what that value is, and respond accordingly.或者,如果您想在 function 中执行逻辑,我只需返回一个值,然后检查该值是什么,并做出相应的响应。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.