× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

Javascript return value from nested functions

Michael Perrenoud
Jul 03, 2015
<p>The standard, accepted way, would be to modify the <code>authenticate</code> function:</p> <pre><code>function authenticate(credentials, cb) { rest.post(BaseURL + '/Users/login', { data : credentials }) .on('complete', function(result, response) { console.log(result.id); // this works fine if (cb) { cb(result.id); } } }); }; </code></pre> <p>and then execute it like this:</p> <pre><code>authenticate({ "username": "test", "password" : "test" }, function(at) { console.log(at); }); </code></pre> <p>Asynchronous calls must be handled with either a callback or a promise. Promises are "pretty" but are nothing more than callbacks. This is why you see almost every single Node API leveraging callbacks.</p> <p>This tip was originally posted on <a href="http://stackoverflow.com/questions/31151292/Javascript%20return%20value%20from%20nested%20functions/31151366">Stack Overflow</a>.</p>
comments powered by Disqus