我可以暂停功能执行,直到一定的价值已经在节点已收到?
有没有办法挂/在其执行过程中暂停的功能,然后继续后几件事情是(从通过socket.io接收到用户输入)的节点JS处理?我知道这是不好的做法,并需要使用的承诺,异步/ AWAIT等。但是,根据我的程序的功能,我需要这样处理。
我在这里我分享我的代码原来的问题是在这里:Node application interactively get user input from react front end by pausing the current execution
回答如下:...“暂停在其执行过程中的功能”是不可能真正描述你想有发生什么。我假设你有运行,负责让你的程序在那里“的几件事情的处理”点一些异步代码...所以你的代码看起来像
var a_few_things_have_been_handled = false;
handle_a_few_things();
// which returns immediately but has a side effect of
// effectively setting a_few_things_have_been_handled to true
while(!a_few_things_have_been_handled) {
// do nothing just wait...
// actually you want to yield to asynchronous threads
// but you can't do it like this
}
the_rest_of_your_program();
不幸的是这就是语言不是如何工作......你要调整你的程序流程更明确一些连续的程序流使用的承诺或类似的异步流程控制结构。