Created
April 24, 2016 08:54
-
-
Save lwdgit/74b3e7417e6a853ce9c5466a51841cad to your computer and use it in GitHub Desktop.
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* wrap() { | |
| yield sleep(1000); | |
| yield sleep(2000); | |
| } | |
| function sleep(time) { | |
| return function(done) { | |
| setTimeout(function() { | |
| console.log('sleep ' + time + 'ms'); | |
| done(null, time); | |
| }, time); | |
| } | |
| } | |
| function co(generator, err, res) { | |
| var ret = generator.next(res); | |
| if (ret.done) return; | |
| ret.value(function(err, res) { | |
| co(generator, err, res); | |
| }); | |
| } | |
| co(wrap()); | |
| function* wrap2() { | |
| yield sleep2.call(this, 4000); | |
| yield sleep2.call(this, 1000); | |
| } | |
| function sleep2(time) { | |
| setTimeout(function() { | |
| console.log('sleep ' + time + 'ms'); | |
| this.next(); | |
| }.bind(this), time) | |
| } | |
| function co2(generator) { | |
| new generator().next(); | |
| } | |
| co2(wrap2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment