Created
May 8, 2013 22:02
-
-
Save creationix/5544019 to your computer and use it in GitHub Desktop.
Working version of generator async code sample. Using node from https://github.com/andywingo/node/tree/v8-3.19
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
tim@touchsmart:~/Code$ nvm use v0.11.2-generators | |
Now using node v0.11.2-generators | |
tim@touchsmart:~/Code$ node --harmony testgen.js | |
<Buffer 76 61 72 20 66 73 20 3d 20 72 65 71 75 69 72 65 28 27 66 73 27 29 3b 0a 66 75 6e 63 74 69 6f 6e 20 72 65 61 64 46 69 6c 65 28 70 61 74 68 2c 20 65 6e 63 ...> | |
Sleeping for 2000ms... | |
Done |
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
var fs = require('fs'); | |
function readFile(path, encoding) { | |
return function (callback) { | |
fs.readFile(path, encoding, callback); | |
}; | |
} | |
function sleep(ms) { | |
return function (callback) { | |
setTimeout(callback, ms); | |
}; | |
} | |
function run(makeGenerator) { | |
return function () { | |
var generator = makeGenerator.apply(this, arguments); | |
var continuable, sync, value; | |
next(); | |
function next() { | |
while (!(continuable = generator.send(value)).done) { | |
continuable = continuable.value; | |
sync = undefined; | |
continuable(callback); | |
if (sync === undefined) { | |
sync = false; | |
break; | |
} | |
} | |
} | |
function callback(err, val) { | |
if (err) return generator.throw(err); | |
value = val; | |
if (sync === undefined) { | |
sync = true; | |
} | |
else { | |
next(); | |
} | |
} | |
} | |
} | |
run(function* () { | |
console.log(yield readFile(__filename)); | |
console.log("Sleeping for 2000ms..."); | |
yield sleep(2000); | |
console.log("Done"); | |
})(); |
callback also can be synchronous,just like
function foo(callback) {
return callback();
}
- The fact if the
sync
istrue
offalse
is never used. So, we don't needundefined
. undefined
can actually be defined in an outer scope. Always use a localvar undefined
.- When the callback was not run synchronously and we've
break
ed from the loop, we call anext
recursively. That's not a very good idea, because you can get out of stack if there're too many asynchronous callbacks in each other. I couldn't find a way to counter this issue, though. (How do we use trampolining here at all?) That's what I've been expecting to see in ES6 from the box.
EDIT. From the fact that those variables are defined outside of next
I'm guessing that you already know about (3) and you've been trying to save stack space. Haven't you?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yes, can you explain the logic of sync? It does not seem consistent.
Secondly, Dao007forever is correct, the callback would not be called until a later tick, so sync is not needed anyway.