Last active
December 29, 2015 11:29
-
-
Save laser/7664049 to your computer and use it in GitHub Desktop.
ES6 Generators
This file contains hidden or 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
setTimeout(function(){ | |
_get("/something.ajax?greeting", function(err, greeting) { | |
if (err) { console.log(err); throw err; } | |
_get("/else.ajax?who&greeting="+greeting, function(err, who) { | |
if (err) { console.log(err); throw err; } | |
console.log(greeting+" "+who); | |
}); | |
}); | |
}, 1000); |
This file contains hidden or 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
genny.run(function* (resume) { | |
_get("test1.txt", resume()); | |
_get("test2.txt", resume()); | |
var res1 = yield resume, res2 = yield resume; // step 1 | |
var res3 = yield _get("test3.txt", resume()); // step 2 | |
console.log(res1 + res2); | |
}); |
This file contains hidden or 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
sync(function* (resume) { | |
try (e) { | |
yield setTimeout(resume, 1000); | |
var greeting = yield _get('/something.ajax?greeting', resume) | |
var who = yield _get('/else.ajax?who&greeting=' + greeting, resume) | |
console.log(greeting + ' ' + who) | |
} | |
catch (e) { | |
console.log(e); | |
throw e; | |
} | |
}); |
This file contains hidden or 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
function* fibonacci() { | |
var a = 0, b = 1, c = 0; | |
while (true) { | |
yield a; | |
c = a; | |
a = b; | |
b = c + b; | |
} | |
} | |
function run() { | |
var seq = fibonacci(); | |
console.log(seq.next().value); // 0 | |
console.log(seq.next().value); // 1 | |
console.log(seq.next().value); // 1 | |
console.log(seq.next().value); // 2 | |
console.log(seq.next().value); // 3 | |
console.log(seq.next().value); // 5 | |
} | |
run(); |
This file contains hidden or 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
function* powGenerator() { | |
var result = Math.pow(yield "a", yield "b"); | |
return result; | |
} | |
var g = powGenerator(); | |
console.log(g.next().value); // "a", from the first yield | |
console.log(g.next(10).value); // "b", from the second | |
console.log(g.next(2).value); // 100, the result |
This file contains hidden or 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
function* powGenerator() { | |
var result = Math.pow(----10----, yield "b"); | |
return result; | |
} |
This file contains hidden or 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
function* powGenerator() { | |
var result = Math.pow(----10----, ----2----); | |
return result; | |
} |
This file contains hidden or 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
// ************** | |
// framework code | |
function sync(gen) { | |
var iterable, resume; | |
resume = function(err, retVal) { | |
if (err) iterable.raise(err); | |
iterable.next(retVal); // raise! | |
}; | |
iterable = gen(resume); | |
iterable.next(); | |
} | |
function _get(url, callback) { | |
var x = new XMLHttpRequest(); | |
x.onreadystatechange = function() { | |
if (x.readyState == 4) { | |
callback(null, x.responseText); | |
} | |
}; | |
x.open("GET", url); | |
x.send(); | |
} | |
// **************** | |
// application code | |
sync(function* (resume) { | |
log('foo'); | |
var resp = yield _get("http://localhost:8080/blix.txt", resume); // suspend! | |
log(resp); | |
}); | |
log('bar'); // not part of our generator function’s body |
This file contains hidden or 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
try { | |
firstAsync(function(err, a) { | |
if (err) { console.log(err); throw err; } | |
secondAsync(function(err, b) { | |
if (err) { console.log(err); throw err; } | |
thirdAsync(function(err, c) { | |
if (err) { console.log(err); throw err; } | |
callback(a, b, c); | |
}); | |
}); | |
}); | |
} | |
catch (e) { | |
console.log(e); | |
} |
This file contains hidden or 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
function sync(gen) { | |
var iterable, resume; | |
resume = function(err, retVal) { | |
if (err) iterable.raise(err); // raise! | |
iterable.next(retVal); | |
}; | |
iterable = gen(resume); | |
iterable.next(); | |
} | |
sync(function* (resume) { | |
try { | |
var x = firstAsync(resume); | |
var y = secondAsync(resume); | |
var z = thirdAsync(resume); | |
// … do something with your data | |
} | |
catch (e) { | |
console.log(e); // will catch errors from any of the three calls | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment