Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does node.js support yield?

Is there any way to get generators into node.js?

I'm currently faking them with callbacks, but I have to remember to check the response of the callback inside of my generator function which creates a lot of if (callback(arg) === false) return;

I want something like in python:

for p in primes():   if p > 100: break   do_something(p) 

which I'm doing in node like this:

primes(function(p) {   if (p > 100) return false;   do_something(p) }); 

Maybe something like coffeescript could help?

like image 485
Paul Tarjan Avatar asked Nov 08 '10 18:11

Paul Tarjan


People also ask

What is yield in node JS?

The yield keyword pauses generator function execution and the value of the expression following the yield keyword is returned to the generator's caller. It can be thought of as a generator-based version of the return keyword. yield can only be called directly from the generator function that contains it.

What is Yield * in JavaScript?

yield keyword is used to resume or pause a generator function asynchronously. A generator function is just like a normal function but the difference is that whenever the function is returning any value, it does it with the help of 'yield' keyword instead of return it.

Why is node js so efficient?

It's a light, scalable, and cross-platform way to execute code. It uses an event-driven I/O model which makes it extremely efficient and makes scalable network applications possible. With more than three billion downloads as of 2022, Node.

What is node js most used for?

Node. js is primarily used for non-blocking, event-driven servers, due to its single-threaded nature. It's used for traditional web sites and back-end API services, but was designed with real-time, push-based architectures in mind.


2 Answers

Yes, since version 0.11. Enjoy!

http://wingolog.org/archives/2013/05/08/generators-in-v8

http://jlongster.com/A-Study-on-Solving-Callbacks-with-JavaScript-Generators

like image 173
Diego Pino Avatar answered Sep 19 '22 23:09

Diego Pino


The answer is "not currently" but Marcel seems to be my hero. Lets hope this goes somewhere:

https://groups.google.com/forum/#!msg/nodejs/BNs3OsDYsYw/oCsWBw9AWC0J https://github.com/laverdet/node-fibers

like image 21
Paul Tarjan Avatar answered Sep 21 '22 23:09

Paul Tarjan