node.js - Converting to synchronous-style a callback hell required by an API -


i trying integrate kurento meteor. facing problems in converting nested callbacks of node.js proper meteor server code.
below code trying convert using meteor.wrapasync :

kurento(args.ws_uri, function(error, client) {   if ( error ) return onerror(error);    client.create('mediapipeline', function(error, pipeline) {     if ( error ) return onerror(error);      console.log("got mediapipeline");      pipeline.create('recorderendpoint', { uri : file_uri }, function(error, recorder) {       if ( error ) return onerror(error);        console.log("got recorderendpoint");        pipeline.create('webrtcendpoint', function(error, webrtc) {         if ( error ) return onerror(error);          console.log("got webrtcendpoint");          webrtc.connect(recorder, function(error) {           if ( error ) return onerror(error);            console.log("connected");            recorder.record(function(error) {             if ( error ) return onerror(error);              console.log("record");              webrtc.connect(webrtc, function(error) {               if ( error ) return onerror(error);                console.log("second connect");             });              webrtc.processoffer(offer, function(error, answer) {               if ( error ) return onerror(error);                console.log("offer");                return answer;             });           });         });       });     });   }); }); 

i trying write in meteor server using wrapasync below.

client = meteor.wrapasync(kurento,ws_uri); //client = meteor.wrapasync(getkurentoclient); console.log("got connected server");  pipeline = meteor.wrapasync(client.create,'mediapipeline'); console.log("got mediapipeline");  var webrtc = meteor.wrapasync(pipeline.create,'webrtcendpoint'); console.log("got webrtcendpoint");  var recorder = meteor.wrapasync(pipeline.create,('recorderendpoint', {uri: file_uri})); console.log("got recorderendpoint");  meteor.wrapasync(webrtc.connect,recorder); console.log("connected recorder");  meteor.wrapasync(webrtc.connect,webrtc); console.log("connected webrtc");  meteor.wrapasync(recorder.record); console.log("started recording");  var sdpanswer = meteor.wrapasync(webrtc.processoffer,offer); console.log("sdpanswer"+sdpanswer()); return sdpanswer; 

whenever trying this, getting function output, not object!. below output of last concole log statement see sdpanswer.

exception while invoking method 'onoffer' typeerror: cannot call method 'apply' of undefined
i20150722-19:10:15.185(5.5)? @ packages/meteor/helpers.js:118:1
i20150722-19:10:15.186(5.5)? @ [object object].meteor.methods.onoffer (app/absimpl.js:90:31)
i20150722-19:10:15.186(5.5)? @ maybeauditargumentchecks (packages/ddp/livedata_server.js:1617:1)
i20150722-19:10:15.186(5.5)? @ packages/ddp/livedata_server.js:648:1
i20150722-19:10:15.186(5.5)? @ [object object]._.extend.withvalue (packages/meteor/dynamics_nodejs.js:56:1)
i20150722-19:10:15.186(5.5)? @ packages/ddp/livedata_server.js:647:1
i20150722-19:10:15.186(5.5)? @ [object object]._.extend.withvalue (packages/meteor/dynamics_nodejs.js:56:1)
i20150722-19:10:15.186(5.5)? @ [object object]._.extend.protocol_handlers.method (packages/ddp/livedata_server.js:646:1)
i20150722-19:10:15.186(5.5)? @ packages/ddp/livedata_server.js:546:1

how can convert callback hell nice synchronous function using meteor.wrapasync?

each callback needs 2 lines, you're trying declare & call @ same time. (http://docs.meteor.com/#/full/meteor_wrapasync)

the first line create synced call. second line call synced call.

clientsync = meteor.wrapasync(client.create, client); pipeline = clientsync('mediapipeline'); 

note haven't used kurento can't go specifics, general wrapasync layout, want.


Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -