睡眠主线程但不阻塞回调
Sleep main thread but do not block callbacks
此代码有效,因为 system-sleep blocks execution of the main thread but does not block callbacks. However, I am concerned that system-sleep is not 100% portable because it relies on the deasync npm 模块依赖于 C++。
是否有系统睡眠的替代方案?
var sleep = require('system-sleep')
var done = false
setTimeout(function() {
done = true
}, 1000)
while (!done) {
sleep(100) // without this line the while loop causes problems because it is a spin wait
console.log('sleeping')
}
console.log('If this is displayed then it works!')
PS 理想情况下,我想要一个适用于 Node 4+ 的解决方案,但总比没有好。
PPS 我知道睡觉不是最好的做法,但我不在乎。我厌倦了反对睡觉的争论。
var one = 0;
function delay(){
return new Promise((resolve, reject) => {
setTimeout(function(){
resolve('resolved')
}, 2000);
})
}
while (one == 0) {
one = 1;
async function f1(){
var x = await delay();
if(x == 'resolved'){
x = '';
one = 0;
console.log('resolved');
//all other handlers go here...
//all of the program that you want to be affected by sleep()
f1();
}
}
f1();
}
根据您的要求将我的评论收集到答案中:
好吧,deasync
(sleep()
取决于)使用了相当多的技巧。它是一个本机代码 node.js 附加组件,可以手动 运行 从 C++ 代码中设置事件循环,以便完成它正在做的事情。只有真正了解 node.js 内部结构(现在和将来)的人才能想象这样做的问题所在。如果不破解 node.js 本机代码,您所要求的在常规 Javascript 代码中是不可能的,因为它与 Javascript 在 node.js 中被设计为 运行 的方式完全相反=].
Understood and thanks. I am trying to write a more reliable deasync (which fails on some platforms) module that doesn't use a hack. Obviously this approach I've given is not the answer. I want it to support Node 4. I'm thinking of using yield / async combined with babel now but I'm not sure that's what I'm after either. I need something that will wait until the callback is callback is resolved and then return the value from the async callback.
Babel 使用 async/await
所做的只是为您编写常规 promise.then()
代码。 async/await
是语法上的便利。他们并没有真正做任何你不能使用 promises、.then()
、.catch()
和在某些情况下 Promise.all()
自己写的东西。所以,是的,如果你想为节点 4 编写 async/await
样式的代码,那么你可以使用 Babel 将你的代码转换为将在节点 4 上 运行 的代码。你可以查看转换后的 Babel 代码使用 async/await
时,您只会找到常规的 promise.then()
代码。
没有 deasync
解决方案不是对引擎的破解,因为引擎并非设计用于执行 deasync 的功能。
node.js 中的 Javascript 被设计为一次 运行 一个 Javascript 事件,代码 运行s 直到它 returns将控制权交还给系统,然后系统将从事件队列中提取下一个事件并 运行 其回调。您的 Javascript 是单线程的,设计上没有先发制人的中断。
如果不对 JS 引擎进行某种破解,您将无法暂停或休眠一件 Javascript 然后 运行 其他事件。它根本不是为了这样做而设计的。
此代码有效,因为 system-sleep blocks execution of the main thread but does not block callbacks. However, I am concerned that system-sleep is not 100% portable because it relies on the deasync npm 模块依赖于 C++。
是否有系统睡眠的替代方案?
var sleep = require('system-sleep')
var done = false
setTimeout(function() {
done = true
}, 1000)
while (!done) {
sleep(100) // without this line the while loop causes problems because it is a spin wait
console.log('sleeping')
}
console.log('If this is displayed then it works!')
PS 理想情况下,我想要一个适用于 Node 4+ 的解决方案,但总比没有好。
PPS 我知道睡觉不是最好的做法,但我不在乎。我厌倦了反对睡觉的争论。
var one = 0;
function delay(){
return new Promise((resolve, reject) => {
setTimeout(function(){
resolve('resolved')
}, 2000);
})
}
while (one == 0) {
one = 1;
async function f1(){
var x = await delay();
if(x == 'resolved'){
x = '';
one = 0;
console.log('resolved');
//all other handlers go here...
//all of the program that you want to be affected by sleep()
f1();
}
}
f1();
}
根据您的要求将我的评论收集到答案中:
好吧,deasync
(sleep()
取决于)使用了相当多的技巧。它是一个本机代码 node.js 附加组件,可以手动 运行 从 C++ 代码中设置事件循环,以便完成它正在做的事情。只有真正了解 node.js 内部结构(现在和将来)的人才能想象这样做的问题所在。如果不破解 node.js 本机代码,您所要求的在常规 Javascript 代码中是不可能的,因为它与 Javascript 在 node.js 中被设计为 运行 的方式完全相反=].
Understood and thanks. I am trying to write a more reliable deasync (which fails on some platforms) module that doesn't use a hack. Obviously this approach I've given is not the answer. I want it to support Node 4. I'm thinking of using yield / async combined with babel now but I'm not sure that's what I'm after either. I need something that will wait until the callback is callback is resolved and then return the value from the async callback.
Babel 使用 async/await
所做的只是为您编写常规 promise.then()
代码。 async/await
是语法上的便利。他们并没有真正做任何你不能使用 promises、.then()
、.catch()
和在某些情况下 Promise.all()
自己写的东西。所以,是的,如果你想为节点 4 编写 async/await
样式的代码,那么你可以使用 Babel 将你的代码转换为将在节点 4 上 运行 的代码。你可以查看转换后的 Babel 代码使用 async/await
时,您只会找到常规的 promise.then()
代码。
没有 deasync
解决方案不是对引擎的破解,因为引擎并非设计用于执行 deasync 的功能。
Javascript 被设计为一次 运行 一个 Javascript 事件,代码 运行s 直到它 returns将控制权交还给系统,然后系统将从事件队列中提取下一个事件并 运行 其回调。您的 Javascript 是单线程的,设计上没有先发制人的中断。 如果不对 JS 引擎进行某种破解,您将无法暂停或休眠一件 Javascript 然后 运行 其他事件。它根本不是为了这样做而设计的。