Dynamic Promise?
#36682
Replies: 1 comment
-
Why not extract the function syncFn() {
return 1
}
async function asyncFn() {
return new Promise((resolve) => {
resolve(0);
});
}
if(condition){
syncFn()
} else {
await asyncFn()
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
When the function is called, a
Promise
is generated, it's very simple and clear.But if this function is called multiple times (for example, 1 million times), and in most cases it will hit the if statement, it will produce a lot of unnecessary performance loss.
I have to do like this:
I think it can return a base type instead of
Promise
, we also don’t need to useawait
to get the result.It can return
Promise
when the return type is reallyPromise
.Should the interpreter optimize the code?
Beta Was this translation helpful? Give feedback.
All reactions