0

Here is the code:

adjust_geo = function(callback){Plasma.Hose('edge-to-sluice').Deposit({ "descrips": [ "sluice", "prot-spec v1.0", "request", "zoom" ], "ingests": { "lat": stickie.latitude, "lon": stickie.longitude, "level": stickie.zoom_level } }); Plasma.Hose('edge-to-sluice').Deposit({ "descrips": [ "sluice", "prot-spec v1.0", "request", "set-time" ], "ingests": { "time": stickie.start, "rate": 2.0, "pause": false } }); callback(); } var remove_stickies = function(callback){ Plasma.Hose('edge-to-sluice').Deposit({ descrips: ['sluice', 'prot-spec v1.0', 'request', 'remove-all-fluoro'], ingests : {} }); callback(); } deposit_fluoros = function(){ console.log(stickie.fluoroscopes); L = JSON.parse(stickie.fluoroscopes); console.log(L); for (var i = 1; i<L.length;i++){ console.log(L[i]); Plasma.Hose('tcp://localhost/edge-to-sluice') .Deposit({ "descrips": [ "sluice", "prot-spec v1.0", "request", "new-fluoro-instance"], "ingests":L[i] }); }; } } adjust_geo(remove_stickies(deposit_fluoros())); 

as you can see on the last line, I am attempting to execute the three asnychronous functions in order. However right now it is complaining that callback() on line 34 is undefined. TypeError: undefined is not a function. Its strange because it doesn't seem to care about the one in adjust_geo?

8
  • 2
    Promises is the way to go. Avoid callbacks for async functions, it'll confuse you quickly. Commented Aug 28, 2014 at 18:41
  • 1
    The reason you're getting the error on 34 is because callback() is called outside the remove_stickies function. Commented Aug 28, 2014 at 18:46
  • what is the desired order of execution of adjust_geo, remove_stickies and deposit_fluoros? Commented Aug 28, 2014 at 19:52
  • @Igor it should be adjust_geo, then remove_stickies, then deposit_fluoros Commented Aug 28, 2014 at 21:42
  • @user3727514 - then VitaliyG's response is your answer Commented Aug 28, 2014 at 21:46

3 Answers 3

1

There is problems with your last line (you have to pass callback function and not make function call) and you have pass callback into all functions or check in code if callback was passed:

adjust_geo( function(){ remove_stickies( deposit_fluoros ) }); 

This will call adjust_geo with callback that will call remove_stickies with callback that will call deposit_fluoros .

If Plasma.Hose('...').Deposit looks like Plasma.Hose('...').Deposit(data, doneCallback) you can update your code to run functions in order by moving next function calls into doneCallback:

adjust_geo = function(callback) { Plasma.Hose('edge-to-sluice').Deposit({ "descrips": [ "sluice", "prot-spec v1.0", "request", "zoom" ], "ingests": { "lat": stickie.latitude, "lon": stickie.longitude, "level": stickie.zoom_level } }, function() { Plasma.Hose('edge-to-sluice').Deposit({ "descrips": [ "sluice", "prot-spec v1.0", "request", "set-time" ], "ingests": { "time": stickie.start, "rate": 2.0, "pause": false } }, callback) } ); } var remove_stickies = function(callback) { Plasma.Hose('edge-to-sluice').Deposit({ descrips: ['sluice', 'prot-spec v1.0', 'request', 'remove-all-fluoro'], ingests: {} }, callback); } deposit_fluoros = function() { console.log(stickie.fluoroscopes); L = JSON.parse(stickie.fluoroscopes); console.log(L); for (var i = 1; i < L.length; i++) { console.log(L[i]); Plasma.Hose('tcp://localhost/edge-to-sluice') .Deposit({ "descrips": [ "sluice", "prot-spec v1.0", "request", "new-fluoro-instance" ], "ingests": L[i] }); }; } } adjust_geo( function() { remove_stickies( deposit_fluoros ) }); 
Sign up to request clarification or add additional context in comments.

2 Comments

this makes sense to me and now the console is logging adjust_geo, remove_stickies, deposit_fluoros in the correct order. Yet the functions themselves still appear to be executing in the wrong order. Is it possible that the callbacks inside of the functions are executing before that main code block (Plasma.Hose. ....) finishes?
Actually all functions in this question are not asynchronous. I guess Plasma.Hose('...').Deposit is asynchronous, and in this case you have to pass callback inside this functions.
0

Change you last line to read

adjust_geo(remove_stickies(deposit_fluoros)); 

The way you are doing it currently doing it, you are passing the RESULT of deposit_fluoros as opposed to passing a reference to the function.

1 Comment

this will not pass a callback to adjust_geo
0

Removed all other objects, just plain callbacks. Put back your objects and it will work. Key rule whenever you are returning or calling the callback make sure you return it as function.

var adjust_geo = function (callback) { callback(); console.log('adjust_geo'); } // wrap the result of first call into a function and return it var remove_stickies = function (callback) { return function () { callback() console.log('remove_stickies'); }; } var deposit_fluoros = function () { console.log('deposit_fluoros') }; adjust_geo(remove_stickies(deposit_fluoros)); 

Output:

"deposit_fluoros" "remove_stickies" "adjust_geo" 

3 Comments

hmm this doesn't give any undefined errors. but it still seems that it is going in order of remove_stickies, deposit_fluoros, adjust_geo. Maybe I should use promises instead?
Updated my answer- if you add your code wherever there is console.log statements, it will be in order.
Promises also rely on callbacks behind the scene, so it's not really one vs. the other. Also No browsers support promises natively in a reliable way

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.