admin管理员组文章数量:1323029
I am having a hard time finding an answer about a question that puzzles me lately.
should I always await
on a promise ?
what do I mean by that?
Let's say I have a function the returns a promise, but I am not doing anything with the value that is returned from that promise. should I still await it ? or should I just leave it within a try catch
Should I await on function that returns Promise ?
const specialFunction = async () => {
try {
await handleEventsInsertion(body)
} catch (error) {
logger.error(error)
}
}
the handleEventsInsertion
method is the method that actually inserts the events to the database. but i am not doing anything with the promise that this method returns. so should i await anyway ? it results in an unhandled rejection if there is a reject
Is there a scenario where i should not await
on a promise ?
I am having a hard time finding an answer about a question that puzzles me lately.
should I always await
on a promise ?
what do I mean by that?
Let's say I have a function the returns a promise, but I am not doing anything with the value that is returned from that promise. should I still await it ? or should I just leave it within a try catch
Should I await on function that returns Promise ?
const specialFunction = async () => {
try {
await handleEventsInsertion(body)
} catch (error) {
logger.error(error)
}
}
the handleEventsInsertion
method is the method that actually inserts the events to the database. but i am not doing anything with the promise that this method returns. so should i await anyway ? it results in an unhandled rejection if there is a reject
Is there a scenario where i should not await
on a promise ?
- "it results in an unhandled rejection if there is a reject": yes it does. So what will you do? – trincot Commented Oct 3, 2020 at 11:53
-
it results in an unhandled rejection if there is a reject
- this is a statement not a question. – Eitank Commented Oct 3, 2020 at 11:55
1 Answer
Reset to default 8No, you don't always need to await
a promise. But, to ensure proper application flow, you should always be ready to handle any error that occurs.
If you do not await
a promise, you should implement the catch
callback. So either the code as you wrote it in your question, or something along the lines of:
const specialFunction = () => {
handleEventInsertion(body)
.catch(error => {
logger.error(error);
});
}
You can choose to not do that, but as you've already stated: if an error occurs and you do not try
/await
/catch
the promise or .catch
the error, it will be regarded as an unhandled rejection.
Edit:
As for your other question: is there a scenario where you should not await on a promise?
Yes. If you're responding to a user action that needs to report back immediately after triggering something that may take a while, you want that to be handled asynchronously and you don't want to wait for it to finish. But, in that case the asynchronous function you're calling should handle the error. As an example:
const onClick = () => {
handleEventInsertion(body); // asynchronous function
alert("Thanks, we'll take it from here");
};
const handleEventInsertion = async (body) => {
try {
const result = await performLengthyAsyncOperation(body);
alert(`${result} inserted successfully`);
} catch (error) {
logger.error(error);
}
// or using the callbacks instead of try/await/catch:
performLengthyAsyncOperation(body)
.then(result => {
alert(`${result} inserted successfully`);
}).catch(error => {
logger.error(error);
});
};
Edit 2:
If you, for some reason, really don't want to use await
at all, there is an alternative solution: you can trap the unhandled rejection errors.
window.onunhandledrejection = (error) => {
logger.error(error);
};
If you do that, you don't need to catch the error everywhere if the handling is always going to be same. In that case, you'd only await
a promise if you need the result of the function or if a particular error requires special treatment.
本文标签: javascriptshould I always await on every promiseStack Overflow
版权声明:本文标题:javascript - should I always `await` on every promise? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742109931a2421199.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论