Hi,

I'm more onto events with EventEmitter() when I need to do a lot of
async/sync stuff.
It's built in nodejs, and easy to use out of the box, npm is not required.

Example with sync stuff :

function (data, callback) {
  var Events = require("events");
  var event = new Events.EventEmitter();

  event.on("job1", function (data) {
    // do stuff with data
    event.emit("job2", err, data);
  });

  event.on("job2", function (err, data) {
    // do stuff with err and data
    event.emit("done", err, data);
  });

  event.on("done", function (err, data) {
    // do stuff with err and data
    // delete event and remove listeners
    callback(err, data);
  });

  event.emit("job1", data);
}

You get the idea.
For async just call emit after a function is done and keep an array updated
so the "done" handle know when all the functions are executed.

Regards.

Edouard Buschini


15.09.2014, 21:13, "// ravi" <[email protected]>:

On Sep 15, 2014, at 1:01 PM, Alex Kocharin <[email protected]> wrote:


Promises just wrap callbacks. So you had callback hell, now you have
wrapped callback hell. Nice change, huh.



Actually yes, I think it is. Readable code is worth it, in my view.



Promises are really useful for a couple of things like error handling, but
readability isn't one of them.

Quite the opposite, that ".then(...)" noise is even worse than callbacks.
Try to rewrite that example with promises, I'm sure it won't be more
readable.

I think generators are the only clean answer here. I know they aren't even
in stable node, but still, they're worth recommending. Well there are
fibers also, but they seem to be forgotten already.






If you want to turn async code to sync-like code, I would suggest to use
generators and `co` module instead.



I am waiting for the right time to start recommending generators+X to folks
asking these sort of questions. I am not comfortable yet doing so, but I
agree that anyone interested should take a look.

—ravi




-- 
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/6BCCC31B-7FCD-4B20-A045-7FFAC897A53A%40g8o.net
<https://groups.google.com/d/msgid/nodejs/6BCCC31B-7FCD-4B20-A045-7FFAC897A53A%40g8o.net?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.

 --
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/500601410802189%40web21h.yandex.ru
<https://groups.google.com/d/msgid/nodejs/500601410802189%40web21h.yandex.ru?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.

-- 
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/CAAK0_j8S1sLJk5HZejufiAmTAyqbbM0HY2DxzvFM5%2B%2B5cq62Sg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to