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 procrastinatingAdd(x, y) { | |
| var errMsg = "Expected number and got "; | |
| setTimeout(function() { | |
| if (isNaN(x)) gen.throw(new TypeError(errMsg + typeof x)); | |
| if (isNaN(y)) gen.throw(new TypeError(errMsg + typeof y)); | |
| gen.next(x + y); | |
| }, 500); | |
| } | |
| var gen = function* () { |
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
| 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
| 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
| function* fibonacci() { | |
| var a = 0, b = 1, c = 0; | |
| while (true) { | |
| yield a; | |
| c = a; | |
| a = b; | |
| b = c + b; | |
| } | |
| } |
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); // resume! | |
| }; |
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); | |
| }); | |
| }); |