Serialize: a simple node utility to serialize execution of asynchronous functions

75 views
Skip to first unread message

Chaoran Yang

unread,
Jun 21, 2013, 4:39:23 PM6/21/13
to nod...@googlegroups.com
Dear all,

I'm tired of the poor syntax of Step or Async or any other existing flow control libraries. I created a new flow control library in nodejs: Serialize (http://github.com/chaoran/node-serialize).

Here's a brief introduction. There's more in the Github page.

Serialize

A simple node utility to serialize execution of asynchronous functions.

What does it do?

Asynchrony in nodejs is great, except that it makes your code looks horrible because of all the callbacks. If you use synchronous functions, which give you good-looking, easy-to-read code, they will block the thread and make your server not responsive.

Here's serailize to the rescue! serialize converts your asynchronous functions into serialized versions. Serialized functions are executed one after another, without explicitly chaining them with callback functions.serialize does NOT execute the function synchronously (block the thread), it just serialize the execution of asynchronous functions. So that it makes the code looks synchronous, but it is actually ascynhronous underneath.

How to use it?

To create a serialized version of an asynchronous function, call serialize with it. For example, if you want to make serialized versions of fs.writeFile and fs.mkdir, you do:

var serialize = require('serialize');

fs.mkdir = serialize(fs.mkdir);
fs.writeFile = serialize(fs.writeFile);

Then, you can use fs.mkdir and fs.writeFile like they are synchronous functions:

fs.mkdir('new');
fs.mkdir('new/folder');
fs.writeFile('new/folder/hello.txt', "hello world", callback);

These function will be executed one after another, but they will not block the thread as their synchronous versions do. The callback will be invoked after the last call completes.



Mark Hahn

unread,
Jun 21, 2013, 5:12:02 PM6/21/13
to nod...@googlegroups.com
>  fs.mkdir = serialize(fs.mkdir);

I assume you wouldn't really want to clobber fs. Wouldn't every other use of fs.mkdir break? I'd name it fsmkdir or something.
> --
> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to nod...@googlegroups.com
> To unsubscribe from this group, send email to
> nodejs+un...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en
>  
> ---
> You received this message because you are subscribed to the Google Groups "nodejs" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>  
>  

Chaoran Yang

unread,
Jun 21, 2013, 5:55:20 PM6/21/13
to nod...@googlegroups.com


On Friday, June 21, 2013 4:12:02 PM UTC-5, Mark Hahn wrote:
>  fs.mkdir = serialize(fs.mkdir);

I assume you wouldn't really want to clobber fs. Wouldn't every other use of fs.mkdir break? I'd name it fsmkdir or something.

No, other uses of fs.mkdir won't break at all. The serialized version will keep the semantics unchanged. 
If your code can correctly in asynchronous version, they can run correctly in serial version. 

However, I agree clobber fs is bad design practice. But the example is, you know, just for demonstration purpose.

Alex Kocharin

unread,
Jun 22, 2013, 1:50:07 AM6/22/13
to nod...@googlegroups.com

It's exactly that kind of thing streamlinejs does, isn't it?

Chaoran Yang

unread,
Jun 22, 2013, 2:22:53 AM6/22/13
to nod...@googlegroups.com
Sort of. But different. 

streamlinejs will transform your synchronous call and rewrite your source code into asynchronous calls. You need to run it with streamline's "_node", not the simple "node". I guess most people won't use that, unless they really really need to use streamline.

Serialize is a very small flow control library, purely javascript, nothing magic. It's not as full fledged as streamlinejs. But I believe it's powerful enough to help with most cases where people need a flow control utility. And you can serialize any asynchronous call in a program, and the program is still correct. 

Anyway, I don't like to use "_node" to run an "node" app, and I don't like the underscore thing in streamline.js. It's just a personal taste. That's why I wrote "serialize": write the code exactly the way I want it to be, and using the most nonintrusive way.

-Chaoran

--
--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en
 
---
You received this message because you are subscribed to a topic in the Google Groups "nodejs" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/nodejs/oH6XyOJNWwM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to nodejs+un...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages