node.js - why node js donot provide a [Mother]function to call any function asynchronously with a supplied call back -


given node.js boasts of asynchronous event driven model, expecting, should able write nodejs function, e.g simple going through loop, e.g iamlooper() below,
might or might not involve file i/o , pass looping function mother nodejs function e.g invoke(),to pass call functiont e.g happyend() below.

expectation after iamlooper finished ,happyend () invoked nodejs supplied function .

e.g : ==>

 gdata =[]; function iamlooper() { var pi = array;  (var ii = 0 ; ii <4 ; ii ++) {     pi[ii] = 13* ii;;  gdata.push(ii);  } console.log("looper  done -tell callback") ; } function  happyend() { console.log("looper says done");} 

i want invoke iamlooper() , supply happyend @ time of invocation. i.e. looking ready made node function e.g invoke, can called this:

  invoke(iamlooper(), happyend());   if(gdata.length > 0) {console.log("looping has started");} 

in essence invoke should same 2 functions supply have working template of callback execution strategy. invoke being executed async, program progresses beyond invoke before finishes. expectation misguided ? can 1 give me guidance here.

if looking preexisting way of doing callbacks in node, should use event emitters (https://nodejs.org/api/events.html):

var eventemitter = require('events').eventemitter; var eventexample = new eventemitter;  //you can create event listeners: eventexample.on('anevent', function(somedata){     //do somedata });  //to trigger event listener must emit: eventexample.emit('anevent', somedata); 

with code, it'd this:

var eventemitter = require('events').eventemitter; var looper = new eventemitter;  looper.on('invoke', function(data){     var callfunction = data.callfunction;     var finishfunction = data.finishfunction;     var callparameters = data.callparameters;     var finishparameters = data.finishparameters;     if(callparameters == null){         callfunction({callbackpara: finishparameters, callbackfunction: finishfunction});     }     else{         callfunction(callparameters, {callbackparameters: finishparameters, callbackfunction: finishfunction});     } });  looper.on('finish', function(data){     var finishfunction = data.callbackfunction;     var parameters = data.callbackparameters;     if(parameters == null){         finishfunction();     }     else{         finishfunction(parameters);     } });   gdata =[]; function iamlooper(g, callback){     var pi = array;     (var ii = 0 ; ii <4 ; ii ++){           pi[ii] = 13* ii;;         g.push(ii);     }     looper.emit('finish', callback); }  function  happyend() { console.log("looper says done");} 

and call like:

looper.emit('invoke', {callfunction: iamlooper, finishfunction: happyend, callparameters: gdata, finishparameters: null}); 

you can normal callbacks:

gdata =[]; function iamlooper(g, callback){     var pi = array;     (var ii = 0 ; ii <4 ; ii ++){           pi[ii] = 13* ii;;         g.push(ii);     }     callback(); } iamlooper(gdata, function(){ console.log("looper says done");} 

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -

jdbc - Not able to establish database connection in eclipse -