最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Creating Callbacks for required modules in node.js - Stack Overflow

programmeradmin3浏览0评论

Is there any possibilitie for creating some kind of a callback within a module created by my own?

My problem is that I have written a module for my application. Within this module is done some task and now my main-app should get a feedback that the module finished his task.

The following describes what i want but wont work ofcourse...

//module mymod.js
function start()
{
    var done = false;

    //do some tasks
    done = true;
}
exports.done = done;

Main App

var mymod = require("./mymod.js");

while(!mymod.done)
{
    //do some tasks
}

I would be very glad if someone could help me.

PS: I tried out child processes (fork) for this situation but as it seems to copy the whole process I cant access opened OpenCV video captures anymore... :( By using modules I dont run into this problem, but instead I get this one for it ^^

Is there any possibilitie for creating some kind of a callback within a module created by my own?

My problem is that I have written a module for my application. Within this module is done some task and now my main-app should get a feedback that the module finished his task.

The following describes what i want but wont work ofcourse...

//module mymod.js
function start()
{
    var done = false;

    //do some tasks
    done = true;
}
exports.done = done;

Main App

var mymod = require("./mymod.js");

while(!mymod.done)
{
    //do some tasks
}

I would be very glad if someone could help me.

PS: I tried out child processes (fork) for this situation but as it seems to copy the whole process I cant access opened OpenCV video captures anymore... :( By using modules I dont run into this problem, but instead I get this one for it ^^

Share Improve this question asked Aug 22, 2013 at 13:42 Fidel90Fidel90 1,8386 gold badges28 silver badges64 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 9

Yes, you can have a callback from your module.

It's as simple as

function funcWithCallback(args, callback){
    //do stuff

    callback();
}

While I don't know what you are trying to acplish, the while loop looks suspicious. You probably should invest in the async package from npm.

async on github

EDIT: I felt the need to clarify something. While the above function does in fact intend the callback is used instead of the return value, it's not exactly async.

The true async approach is to do something like this:

function funcWithCallback(args, callback){      
    process.nextTick(function() {
        //do stuff
        callback();
    });     
}

This allows the called function to exit and defers the execution of the logic of that function until the next tick.

The call back syntax:

function start(callback)
{


    //do some tasks

    callback(data);
}

exports.start = start;

Anywhere you require your module:

var mymod = require("./mymod.js");

mymod.start(function(data) {
   //do some tasks , process data

});
发布评论

评论列表(0)

  1. 暂无评论