admin管理员组文章数量:1415140
I'm currently teaching myself Javascript/Node.js, especially the concept of a Promise
and asynchronous operations. Recently while looking at some code on Github I've e across a peculiar piece of code that I'd love to have someone explain to me. Basically at the end of a function, there is this:
async myFunction() {
...
// Just wait forever, until signal
await new Promise(() => {});
}
What does the last line actually "do"? The ment above it just makes it even more confusing.
When I tried to execute the code, the function never returned. So I'm curious as to what that is supposed to do. Can someone explain that to me?
I'm currently teaching myself Javascript/Node.js, especially the concept of a Promise
and asynchronous operations. Recently while looking at some code on Github I've e across a peculiar piece of code that I'd love to have someone explain to me. Basically at the end of a function, there is this:
async myFunction() {
...
// Just wait forever, until signal
await new Promise(() => {});
}
What does the last line actually "do"? The ment above it just makes it even more confusing.
When I tried to execute the code, the function never returned. So I'm curious as to what that is supposed to do. Can someone explain that to me?
Share Improve this question asked Oct 12, 2022 at 7:50 87dsf897w6543d54fsdf87dsf897w6543d54fsdf 732 silver badges6 bronze badges 9- 1 as written, waits forever – Jaromanda X Commented Oct 12, 2022 at 7:54
-
1
the ment
Just wait forever
is obvious ...until signal
not sure what that means, guess it depends entirely on the code you haven't shown – Jaromanda X Commented Oct 12, 2022 at 7:55 - 1 The ment and your experiment both tell you the same thing the implementation does, it neither rejects nor resolves so never settles and the await never ends, so it's unclear how that's resulted in confusion. The signal is presumably from the OS to Node, e.g. you ending the loop by quitting the program. – jonrsharpe Commented Oct 12, 2022 at 7:56
- 1 @JaromandaX the only thing I can think of is signal kill or similar OS-level signals the control an application. But it's still a mystery whether the ment meant that or not. And why would you ment that you wait for OS-level process termination. – VLAZ Commented Oct 12, 2022 at 7:57
- 1 Just link the source code (on GitHub, in the line of interest, click on the three dots then "copy permalink"), so people here can have a look at the actual code and find out why it's written that way. – Gerardo Furtado Commented Oct 12, 2022 at 7:59
2 Answers
Reset to default 4When you run the code, the Promise will begin executing and awaiting a response from the inner method signature () => {}
. Until a response es back, the Promise will then be taken into the status Pending. However, you can have a Promise that it'll never be resolved.
What () => {}
means is, give a set of empty parameters ()
, pass =>
them into the empty body {}
for use. But since there are no parameters and the body is empty, we are essentially stuck. The Promise will never fulfill the await
because nothing es back - there is no return value.
Typically when you create a Promise, you'll want to bring in two parameters (resolve, reject), which would lead to two pathways, a pass or fail. Fulfilling either will get the Promise out of the pending state, pleting the request.
Read up on this link here - Mozilla documentation on using promises
The key takeaway mentioned here on that site is that a value must always be returned. The code you are questioning, await new Promise(() => {});
will not return anything, but that will not stop other pieces of your code from executing during the await, so it's probably there to keep some process alive.
Another interesting note is when you add a then()
to that:
async myFunction() {
await new Promise(() => {}).then(() => {
console.log("I will never print to console log");
});
console.log("me neither, due to being scoped to await/async");
}
myFunction();
The then()
will never execute, nor anything after that. But if you take the async\await
out...
myFunction() {
new Promise(() => {}).then(() => {
console.log("I will never print to console log");
});
console.log("You'll see me in the console log");
}
myFunction();
console.log("You'll also see me in the console log too");
Anything after that promise will run.
This is because this promise is not resolving or rejecting and remains forever in pending state.
await new Promise((resolve) => { resolve()});
this above code will terminate your code because promise is resolved. see if this helps.
本文标签: javascriptWhat does quotawait new Promise(() gt )quot doStack Overflow
版权声明:本文标题:javascript - What does "await new Promise(() => {});" do? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745202220a2647441.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论