如何在 Node.js 上阻止进程并等待结果?
How to block process and wait for result on Node.js?
我遇到了 Node.js (v0.12.7) 的问题。我正在编写一个应用程序,它必须在某处停止并等待查询数据库的结果,然后等待它在做什么。
而且问题并不像使用异步(系列等...)那么简单。我有多个函数和模块相互调用。我尝试使用异步,但它没有解决我的问题。
具体来说,我有一个类似的方案:
db = require('db-utils');
exports.myFunction() {
// some code ...
while(condition) {
for(i from 0 to x) {
// code ...
if(condition) {
// code ...
db.executeCall(callback); // async with callback
// this where I want to stop waiting for result
// continue doing something with the result, in my case it is jumping,
// so I have a result = undefined
// code ..
}
// code ...
}
// code ...
}
// code ...
// calculating some properties of oResult from the db query result above
return oResult;
}
理想的情况是将文件的所有内容作为顺序代码执行,但除了 db 调用之外,所有其他的都应该正常工作(我假设,一些 for,if,变量赋值,没什么特别的) .而且,我不能将所有内容都放在回调中,因为在 if、for 和 while 之外还有代码...
我找到了 wait.for,但它似乎对我不起作用(并且该项目似乎已被放弃,因为最后一次提交是在 2 年前:/)
我知道所问的是反对 Node.js 范式的。那个不应该阻止等待异步调用的进程,但有时您只需要这样做(想想 "refactoring 5000 lines of code written for another platform by other persons and you have to port it to node" --vs-- "blocking the process for 10ms waiting for a pitiful db call")。
所以对于那些为了该死的截止日期和资源限制而愿意违背最佳实践和做事方式的人,也许只是为了好玩,并准备好反对愤怒的人数百万 nodejs-async-warriors,欢迎来到 DEASYNC.
的世界
deasync turns async function into sync, implemented with a blocking mechanism by calling Node.js event loop at JavaScript layer. The core of deasync is writen in C++.
如前所述,它会干扰 Node.js 的低级别以实现真正的进程阻塞。希望对有需要的人有所帮助
我的搜索让我发现其他 "solutions" 试图解决在 Node.js 中编写同步调用的问题,但 none 适合我的情况。也许它会在其他情况下起作用,所以在跳到像 DEASYNC:
这样的激进措施之前检查它们会很好
- Wait.for: github.com/luciotato/waitfor & Wait.for-es6 : github.com/luciotato/waitfor-ES6 =>
Sequential programming for node.js and the browser, end of callback hell.
Simple, straightforward abstraction.
By using wait.for, you can call any nodejs standard async function in sequential/Sync mode, waiting for result data, without blocking node's event loop.
- 纤维:github。com/laverdet/node-fibers
- Streamline.js: github.com/Sage/streamlinejs
- 简单 ES6 生成器:chrisbuttery.com/articles/synchronous-asynchronous-javascript-with-es6-generators/
- 著名的Async:github.com/caolan/async
我希望这将用于善而不是恶。
以生成器的方式为基础,现在这个简单的模式在很多情况下都是很好的解决方案:
// nodejs script doing sequential prompts using async readline.question function
var main = (function* () {
// just import and initialize 'readline' in nodejs
var r = require('readline')
var rl = r.createInterface({input: process.stdin, output: process.stdout })
// magic here, the callback is the iterator.next
var answerA = yield rl.question('do you want this? ', r=>main.next(r))
// and again, in a sync fashion
var answerB = yield rl.question('are you sure? ', r=>main.next(r))
// readline boilerplate
rl.close()
console.log(answerA, answerB)
})() // <-- executed: iterator created from generator
main.next() // kick off the iterator,
// runs until the first 'yield', including rightmost code
// and waits until another main.next() happens
我遇到了 Node.js (v0.12.7) 的问题。我正在编写一个应用程序,它必须在某处停止并等待查询数据库的结果,然后等待它在做什么。
而且问题并不像使用异步(系列等...)那么简单。我有多个函数和模块相互调用。我尝试使用异步,但它没有解决我的问题。
具体来说,我有一个类似的方案:
db = require('db-utils');
exports.myFunction() {
// some code ...
while(condition) {
for(i from 0 to x) {
// code ...
if(condition) {
// code ...
db.executeCall(callback); // async with callback
// this where I want to stop waiting for result
// continue doing something with the result, in my case it is jumping,
// so I have a result = undefined
// code ..
}
// code ...
}
// code ...
}
// code ...
// calculating some properties of oResult from the db query result above
return oResult;
}
理想的情况是将文件的所有内容作为顺序代码执行,但除了 db 调用之外,所有其他的都应该正常工作(我假设,一些 for,if,变量赋值,没什么特别的) .而且,我不能将所有内容都放在回调中,因为在 if、for 和 while 之外还有代码...
我找到了 wait.for,但它似乎对我不起作用(并且该项目似乎已被放弃,因为最后一次提交是在 2 年前:/)
我知道所问的是反对 Node.js 范式的。那个不应该阻止等待异步调用的进程,但有时您只需要这样做(想想 "refactoring 5000 lines of code written for another platform by other persons and you have to port it to node" --vs-- "blocking the process for 10ms waiting for a pitiful db call")。
所以对于那些为了该死的截止日期和资源限制而愿意违背最佳实践和做事方式的人,也许只是为了好玩,并准备好反对愤怒的人数百万 nodejs-async-warriors,欢迎来到 DEASYNC.
的世界deasync turns async function into sync, implemented with a blocking mechanism by calling Node.js event loop at JavaScript layer. The core of deasync is writen in C++.
如前所述,它会干扰 Node.js 的低级别以实现真正的进程阻塞。希望对有需要的人有所帮助
我的搜索让我发现其他 "solutions" 试图解决在 Node.js 中编写同步调用的问题,但 none 适合我的情况。也许它会在其他情况下起作用,所以在跳到像 DEASYNC:
这样的激进措施之前检查它们会很好- Wait.for: github.com/luciotato/waitfor & Wait.for-es6 : github.com/luciotato/waitfor-ES6 =>
Sequential programming for node.js and the browser, end of callback hell. Simple, straightforward abstraction. By using wait.for, you can call any nodejs standard async function in sequential/Sync mode, waiting for result data, without blocking node's event loop.
- 纤维:github。com/laverdet/node-fibers
- Streamline.js: github.com/Sage/streamlinejs
- 简单 ES6 生成器:chrisbuttery.com/articles/synchronous-asynchronous-javascript-with-es6-generators/
- 著名的Async:github.com/caolan/async
我希望这将用于善而不是恶。
以生成器的方式为基础,现在这个简单的模式在很多情况下都是很好的解决方案:
// nodejs script doing sequential prompts using async readline.question function
var main = (function* () {
// just import and initialize 'readline' in nodejs
var r = require('readline')
var rl = r.createInterface({input: process.stdin, output: process.stdout })
// magic here, the callback is the iterator.next
var answerA = yield rl.question('do you want this? ', r=>main.next(r))
// and again, in a sync fashion
var answerB = yield rl.question('are you sure? ', r=>main.next(r))
// readline boilerplate
rl.close()
console.log(answerA, answerB)
})() // <-- executed: iterator created from generator
main.next() // kick off the iterator,
// runs until the first 'yield', including rightmost code
// and waits until another main.next() happens