问题
I am facing a problem with Node.js (v0.12.7). I am writing an application that must be stopped somewhere and wait for a result from a query to database and then what it is doing.
And the problem is not as trivial as using async (series, etc ...). I have multiple functions and modules calling each others. I tried using async but it didn't solve my problem.
Concretely speaking, I have a scheme similar to this :
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;
}
The ideal case, is to execute all the content of the file as sequential code, but other than the db call, all the others should work normally (I assume, some for, if, variable assignment, nothing fancy). And also, I can't put everything in the callback, because there is a code outside the if, for, and while ...
I have found wait.for, but it didn't seem to work for me (and the project seems to be abandoned as the last commit was 2 years ago :/)
回答1:
Building on the generators way, nowadays this simple pattern can be fantastic solution in many situations:
// 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
回答2:
I know that what was asked is against Node.js paradigm. That one should not block the process waiting for an async call, but there are times where you just have to do it (think "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").
So for those who are willing to go against the best practices and how things are done for the sake of the damned deadlines and resource-limitations and maybe just for the fun of it and are ready to stand against the rage of millions of nodejs-async-warriors, you are welcome to the world of 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++.
As it is stated, it interferes with the low levels of Node.js to achieve real process blocking. I hope this will help those who are in need for it.
My search led me to discover other "solutions" trying to solve the problem of writing sync calls in Node.js, but none work for my situation. Maybe it will work in other situation, so it will be good to check them before jumping to radical measures like 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.
- Fibers: github.com/laverdet/node-fibers
- Streamline.js: github.com/Sage/streamlinejs
- Simple ES6 Generators: chrisbuttery.com/articles/synchronous-asynchronous-javascript-with-es6-generators/
- The famous Async: github.com/caolan/async
I hope this will be used on good not in evil.
来源:https://stackoverflow.com/questions/32101398/how-to-block-process-and-wait-for-result-on-node-js