如何编写等待一个事件的“返回”前火的node.js功能? [英] How to write a node.js function that waits for an event to fire before 'returning'?

查看:194
本文介绍了如何编写等待一个事件的“返回”前火的node.js功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个节点的应用程序,是的的Web应用程序 - 它返回时,程序的结果被打印到控制台前返回1.立即之前完成了一系列的异步任务

I have a node application that is not a web application - it completes a series of asynchronous tasks before returning 1. Immediately before returning, the results of the program are printed to the console.

我如何确保所有的异步工作返回之前完成?我可以通过调用res.end()前,确保我们完成了所有任务,实现在Web应用程序与此类似,但我还没有作最后的'事件'让脚本返回之前调用任何等价物。

How do I make sure all the asynchronous work is completed before returning? I was able to achieve something similar to this in a web application by making sure all tasks we completed before calling res.end(), but I haven't any equivalent for a final 'event' to call before letting a script return.

请参阅以下我(打破)目前的功能,试图等到调用堆栈是空的。我刚刚发现,这是一种荒谬的做法,因为节点等待processHub进入任何称为processObjWithRef异步函数之前完成。

See below for my (broken) function currently, attempting to wait until callStack is empty. I just discovered that this is a kind of nonsensical approach because node waits for processHub to complete before entering any of the asynchronous functions called in processObjWithRef.

function processHub(hubFileContents){
    var callStack = [];
    var myNewObj = {};
    processObjWithRef(samplePayload, myNewObj, callStack);
    while(callStack.length>0){
        //do nothing
    }
    return 1
}

请注意:我已经试过很多次previously实现这种像异步库的行为(见我在<一个相关的问题href=\"http://stackoverflow.com/questions/9884418/how-can-i-make-this-call-to-request-in-nodejs-synchronous\">How我能够做这样的在同步的NodeJS要求?),所以请把答案和注释有考虑建议的基础上只使用非同步任何答案之前。

Note: I have tried many times previously to achieve this kind of behavior with libraries like async (see my related question at How can I make this call to request in nodejs synchronous?) so please take the answer and comments there into account before suggesting any answers based on 'just use asynch'.

推荐答案

问题是与你的功能设计。要返回同步结果的形式,是异步执行的任务列表。

The problem is with your design of the function. You want to return a synchronous result form a list of tasks that are executed asynchronously.

您应该一个额外的参数,这将是回调,你会把结果实现你的功能(在这种情况下,1)对一些消费者用它做什么。

You should implement your function with an extra parameter that will be the callback where you would put the result (in this case, 1) for some consumer to do something with it.

此外,你需要在你的内部函数回调参数,否则结束时,你会不知道。如果这最后一件事情是不可能的,那么你应该做一些投票站的(使用setInterval的可能)来测试调用堆栈数组填充时。

Also you need to have a callback parameter in your inner function, otherwise you won't know when it ends. If this last thing is not possible, then you should do some kind of polling (using setInterval perhaps) to test when the callStack array is populated.

记住,在Javascript中你永远也不会做一个忙等待。因为它运行在一个单一的过程,将完全锁定您的程序。

Remember, in Javascript you should never ever do a busy wait. That will lock your program entirely as it runs on a single process.

这篇关于如何编写等待一个事件的“返回”前火的node.js功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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