On Sep 25, 2014, at 12:09 PM, Haroon Dilshad wrote:
>
> Hello everyone, I'm having trouble with getting around asynchronous model of
> node, I have this function
> function getstream() {
>
> console.log('calling testAvail, Avail value is:' +
> available);
> testAvailability();
> console.log('Available:'+available);
> if (available || SelfSharing) {
>
> // Do something
> setDefaults();
>
>
> return;
> }
> }
>
> which calls testAvailability() function defined as
>
> function testAvailability()
> {
> console.log('entered test');
> var stat;
> var socket = io.connect('http://somedomain.com');
> socket.on('error', function() {
>
> console.log('There was an error at server end please try again');
> // chrome.runtime.reload();
>
> setTimeout(chrome.runtime.reload(),3000);
> //here i change options
> //socket = io.connect(host, options);
> });
> socket.emit('available');
> socket.on('available', function (status) {
> console.log('got status from server which is:'+ status);
> available=status;
> console.log("inside the socket.on"+available);
> console.log('leaving test, do you see any got status above?');
> });
>
> }
>
>
> Now in this first function I need the available variable's value but due to
> async nature, my first function continues without getting the value of
> available could someone please suggest some work around? code would help.
>
You need to rethink both of these functions to be asynchronous, and also to not
use global variables. Each function will accept a callback, which the function
will call when it it's ready to do so.
--
Job board: http://jobs.nodejs.org/
New group rules:
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/nodejs/D5748E40-E57E-48B4-B44A-792A90DDE0D6%40ryandesign.com.
For more options, visit https://groups.google.com/d/optout.