Created
November 13, 2013 22:29
-
-
Save tikurahul/7457672 to your computer and use it in GitHub Desktop.
Flow control with Generators
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Please correct me if I am making incorrect assumptions: | |
The way everything works is | |
async(function *() { | |
// generator expression | |
// returns multiple promises | |
var x = yield $.ajax('http://x....'); | |
var y = yield $.ajax('http://y....'); | |
yield {'x': x, 'y': y } | |
}) | |
The generator expression returns a promise at the end of every yeild step. We can set state on the generator using the next() with arguments. | |
The only other usecase - which this does not cover is doing multiple things at the 'same' time i.e creating a barrier where we determine that the only places where x, y are being used after the second yeild statement. Hence its safe to execute the second yeild statement before the resolve / reject of the first promise ? Am i right ? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Kind of, it would be better to say that a generator expression 'yields' multiple promises. You should then change the last
yield
keyword to areturn
and that way it wouldyield
two promises and return an object.You are correct that it doesn't do parallelisation implicitly. What you could do is
yield
on usage rather thanyield
on promise creation, e.g.Doing this would largely automate the parallelisation for you, at the cost of sometimes having extra yields.