0

I try to rewire my brain for async coding and I still I still seem to fail. Unfortunately I can't figure out how to do it properly in this case. Let me give an example:

var nconf = require('nconf');

 nconf.argv()
 .env();

 function runme(callback){
   var nummessages = nconf.get('SMS_MESSAGES');
   decodemessage(0, nummessages);
   callback();
 }

 function decodemessage(i, nummessages) {
  if( i < nummessages ) {
    var message = 'SMS_'+(i+1)+'_TEXT';
    var number = 'SMS_'+(i+1)+'_NUMBER';
    console.log('number: '+number);
    console.log('message: '+message);
    console.log('message text: '+ nconf.get(message));
    console.log('message number: '+ nconf.get(number));
    decodemessage(i+1);
  }
} 

runme(function(){
  process.exit(0);
});

This gets called by gammu-smsd which receives SMS messages and puts them into ENV vars and launches a script. This works fine as long as I run it on my own, return code seems to work as well. When I run it from gammu-smsd it fails and it looks like decodemessage is never executed. Some debug output shows that I do go into runme().

My guess is that the callback in runme() gets executed before the recursive decodemessage() call is done. Am I right and if so can someone explain how I can do that properly?

If you want to test run it like this:

node server.js --SMS_MESSAGES 2 --SMS_1_TEXT blabliblo --SMS_1_NUMBER=47796546546 --SMS_2_TEXT Iammessagetwo --SMS_2_NUMBER 12345678

Thanks

4
  • 1
    No, not if the recursive decodemessage is synchronous. Actually, where is the async part that requires the use of a callback at all? Commented Nov 7, 2012 at 11:28
  • I'm not sure if I understand you correctly, all I want to assure with the callback is that when everything is done and the recursion returns my script exists with 0. Commented Nov 7, 2012 at 13:47
  • 1
    Javascript execution is single-threaded. So if you don't have any asynchronous stuff, you should just use a normal for-loop and place the process.exit(0) statement right after it. If you have async stuff, show it to us (there is none in your example code)! Commented Nov 7, 2012 at 13:56
  • ok I see what you mean. I now did a for-loop edition and while it works in the test it does not from gammu-sms, again it doesn't enter the loop. So I have some weird problem I have to figure out, will report back. I will post data via request later so this will be async. Commented Nov 7, 2012 at 15:24

1 Answer 1

2

Seems like you forgot to pass on the nummessages in your recursive call:

decodemessage(i+1, nummessages);

That's why I personally mostly loop backwards (to zero) when using recursion.

Sign up to request clarification or add additional context in comments.

2 Comments

Tnx that's correct, I had a closure before that's why I forgot to add it. However this does not seem to fix the problem, it still doesn't go into decodemessage when I launch it from gammu-smsd.
I close this one, I'm pretty sure gammu-smsd does something wrong and the code was correct. Thanks for the hints guys.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.