Answer the question
In order to leave comments, you need to log in
How justified is it to use generators in js?
That is, in this way we are no longer working asynchronously.
And we lose the benefits of asynchrony. The question is mainly about generators in the node for queries to the database.
Maybe I misunderstood something
Answer the question
In order to leave comments, you need to log in
Firstly, generators are not about asynchrony (although you can use it for it, but you don’t need to), generators are about interrupted calculations and managed iterators.
For example, with the help of generators, problems for infinite sequences are well solved:
function* fibo() {
let prev = 1;
let prePrev = 1;
yield prePrev;
yield prev;
while(true) {
let cur = prePrev + prev;
yield cur;
prePrev = prev;
prev = cur;
}
}
for(let val of fibo()) {
alert(val);
if(val > 100) { break; }
}
es2018 has asynchronous generators. babe knows how to do them. in a fresh node, firefox and chrome are already supported natively.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question