I've been very excited about Node JS for awhile. I finally decided to knuckle down and write a test project to learn about generators in the latest Harmony build of Node.
Here is my very simple test project:
https://github.com/kirkouimet/project-node
To run my test project, you can easily pull the files from Github and then run it with:
node --harmony App.js
Here's my problem - I can't seem to get Node's asynchronous fs.readdir method to run inline with generators. Other projects out there, such as Galaxy and suspend seem to be able to do it.
Here is the block of code I need to fix. I want to be able to instantiate an object of type FileSystem and call the .list() method on it:
FileSystem = Class.extend({
construct: function() {
this.currentDirectory = null;
},
list: function*(path) {
var list = yield NodeFileSystem.readdir(path);
return list;
}
});
Do I need to do something ahead of time to convert Node's fs.readdir into a generator?
One important note, I am parsing all class functions as they are created. This lets me handle generator functions differently than normal functions:
I've been really stumped with this project. Would love any assistance!
Here is what I am trying to accomplish:
- Heavy use of classes with a modified version of John Resig's JavaScript Class support with inheritance
- Using generators to get inline support for Node's stock async calls
Edit
I've tried to implement your example function and I am running into some trouble.
list: function*(path) {
var list = null;
var whatDoesCoReturn = co(function*() {
list = yield readdir(path);
console.log(list); // This shows an array of files (good!)
return list; // Just my guess that co should get this back, it doesn't
})();
console.log(whatDoesCoReturn); // This returns undefined (sad times)
// I need to use `list` right here
return list; // This returns as null
}
@loganfsmyth already provides a great answer to your question. The goal of my answer is to help you understand how JavaScript generators actually work, as this is a very important step to using them correctly.
Generators implement a state machine, the concept which is nothing new by itself. What's new is that generators allow to use the familiar JavaScript language construct (e.g.,
for
,if
,try/catch
) to implement a state machine without giving up the linear code flow.The original goal for generators is to generate a sequence of data, which has nothing to do with asynchrony. Example:
The second part (
bulkySequence
) shows how to implement the same state machine in the traditional way, without generators. In this case, we no longer able to usewhile
loop to generate values, and the continuation happens vianextStep
callback. This code is bulky and unreadable.Let's introduce asynchrony. In this case, the continuation to the next step of the state machine will be driven not by
for of
loop, but by some external event. I'll use a timer interval as a source of the event, but it may as well be a Node.js operation completion callback, or a promise resolution callback.The idea is to show how it works without using any external libraries (like
Q
,Bluebird
,Co
etc). Nothing stops the generator from self-driving itself to the next step, and that's what the following code does. Once all steps of the asynchronous logic have completed (the 10 timer ticks),doneCallback
will be invoked. Note, I don't return any meaningful data withyield
here. I merely use it to suspend and resume the execution:Finally, let's create a sequence of events:
Once you understand this concept, you can move on with using promises as wrappers for generators, which takes it to the next powerful level.
First and foremost, it is important to have a good model in your head of exactly what a generator is. A generator function is a function that returns a generator object, and that generator object will step through
yield
statements within the generator function as you call.next()
on it.Given that description, you should notice that asynchronous behavior is not mentioned. Any action on a generator on its own is synchronous. You can run to the first
yield
immediately and then do asetTimeout
and then call.next()
to go to the nextyield
, but it is thesetTimeout
that causes asynchronous behavior, not the generator itself.So let's cast this in the light of
fs.readdir
.fs.readdir
is an async function, and using it in a generator on its own will have no effect. Let's look at your example:Hopefully it makes it clearer that what you are still calling
readdir
synchronously, and you are not passing any callback, so it will probably throw an error or something.So how do you get nice behavior from generators?
Generally this is accomplished by having the generator yield a special object that represents the result of
readdir
before the value has actually been calculated.For (unrealistic) example,
yield
ing a function is a simple way to yield something that represents the value.Really, you would want this type of logic to continue calling the generator until all of the
yield
calls are done, rather than hard-coding each call. The main thing to notice with this though, is now the generator itself looks synchronous, and everything outside theread
function is super generic.You need some kind of generator wrapper function that handles this yield value process, and your example of the
suspend
does exactly this. Another example isco
.The standard method for the method of "return something representing the value" is to return a
promise
or athunk
since returning a function like I did is kind of ugly.With the
thunk
andco
libraries, you with do the above without the example function:Update
Your update still has some confusion. If you want your
list
function to be a generator, then you will need to useco
outside oflist
wherever you are calling it. Everything inside ofco
should be generator-based and everything outsideco
should be callback-based.co
does not makelist
automatically asynchronous.co
is used to translate a generator-based async flow control into callback-based flow control.e.g.