如何在Node.js上阻止进程并等待结果? [英] How to block process and wait for result on Node.js?

查看:56
本文介绍了如何在Node.js上阻止进程并等待结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了Node.js(v0.12.7)的问题.我正在编写一个必须停止在某处的应用程序,并等待从查询到数据库的结果,然后再知道它在做什么.

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;
}

理想的情况是将文件的所有内容作为顺序代码执行,但是除了db调用之外,所有其他内容都应该正常工作(我假设,有些是变量赋值的,没有花哨的地方).而且,我不能将所有内容都放在回调中,因为在if,for和while之外还有一个代码...

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 ...

我找到了wait.for,但是它似乎对我不起作用(该项目似乎被放弃了,因为最后一次提交是2年前:/)

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 :/)

推荐答案

我知道所要求的是针对Node.js范式的.那不应该阻止等待异步调用的进程,但是有时候您只需要这样做(请考虑一下重构其他人为另一平台编写的5000行代码,然后将其移植到节点上")vs--将进程等待10ms的时间延迟,可怜的数据库调用").

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").

因此,对于那些愿意违背最佳实践以及如何为限期的截止日期和资源限制而做事的人,也许只是为了好玩而已,并愿意抵制数以百万计的愤怒的人,nodejs-async-warriors,欢迎您来到 DEASYNC .

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通过在JavaScript层调用Node.js事件循环,通过阻止机制将异步功能转换为同步.异步的核心是用C ++编写的.

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的较低级别以实现真正的进程阻塞.我希望这会对有需要的人有所帮助.

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.

我的搜索使我发现了其他解决方案",试图解决在Node.js中编写同步调用的问题,但是没有一种方法适合我的情况.也许在其他情况下也可以使用,因此最好先进行检查,然后再采取诸如 DEASYNC :

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:

  • 等待:github.com/luciotato/waitfor& Wait.for-es6 :github.com/luciotato/waitfor-ES6 =>
  • Wait.for: github.com/luciotato/waitfor & Wait.for-es6: github.com/luciotato/waitfor-ES6 =>

node.js和浏览器的顺序编程,回调地狱的结尾.简单,直接的抽象.通过使用wait.for,您可以在顺序/同步模式下调用任何nodejs标准异步函数,以等待结果数据,而不会阻塞节点的事件循环.

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/
  • 著名的异步:github.com/caolan/async
    • 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.

      这篇关于如何在Node.js上阻止进程并等待结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆