Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Update:</strong></p> <p>Now I would advise to have a look at:</p> <ul> <li><p><strong><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" rel="noreferrer">Promises</a></strong> </p> <blockquote> <p>The Promise object is used for deferred and asynchronous computations. A Promise represents an operation that hasn't completed yet, but is expected in the future.</p> </blockquote> <p>A popular promises library is <a href="http://bluebirdjs.com/docs/getting-started.html" rel="noreferrer">bluebird</a>. A would advise to have a look at <a href="http://bluebirdjs.com/docs/why-promises.html" rel="noreferrer">why promises</a>.</p> <blockquote> <p>You should use promises to turn this:</p> <pre><code>fs.readFile("file.json", function (err, val) { if (err) { console.error("unable to read file"); } else { try { val = JSON.parse(val); console.log(val.success); } catch (e) { console.error("invalid json in file"); } } }); </code></pre> <p>Into this:</p> <pre><code>fs.readFileAsync("file.json").then(JSON.parse).then(function (val) { console.log(val.success); }) .catch(SyntaxError, function (e) { console.error("invalid json in file"); }) .catch(function (e) { console.error("unable to read file"); }); </code></pre> </blockquote></li> <li><p><strong>generators:</strong> For example via <a href="https://github.com/tj/co" rel="noreferrer">co</a>. </p> <blockquote> <p>Generator based control flow goodness for nodejs and the browser, using promises, letting you write non-blocking code in a nice-ish way.</p> <pre><code>var co = require('co'); co(function *(){ // yield any promise var result = yield Promise.resolve(true); }).catch(onerror); co(function *(){ // resolve multiple promises in parallel var a = Promise.resolve(1); var b = Promise.resolve(2); var c = Promise.resolve(3); var res = yield [a, b, c]; console.log(res); // =&gt; [1, 2, 3] }).catch(onerror); // errors can be try/catched co(function *(){ try { yield Promise.reject(new Error('boom')); } catch (err) { console.error(err.message); // "boom" } }).catch(onerror); function onerror(err) { // log any uncaught errors // co will not throw any errors you do not handle!!! // HANDLE ALL YOUR ERRORS!!! console.error(err.stack); } </code></pre> </blockquote></li> </ul> <hr> <p>If I understand correctly I think you should have a look at the very good <a href="https://github.com/caolan/async" rel="noreferrer">async</a> library. You should especially have a look at the <a href="https://github.com/caolan/async#series" rel="noreferrer">series</a>. Just a copy from the snippets from github page:</p> <pre><code>async.series([ function(callback){ // do some stuff ... callback(null, 'one'); }, function(callback){ // do some more stuff ... callback(null, 'two'); }, ], // optional callback function(err, results){ // results is now equal to ['one', 'two'] }); // an example using an object instead of an array async.series({ one: function(callback){ setTimeout(function(){ callback(null, 1); }, 200); }, two: function(callback){ setTimeout(function(){ callback(null, 2); }, 100); }, }, function(err, results) { // results is now equals to: {one: 1, two: 2} }); </code></pre> <p>As a plus this library can also run in the browser.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload