npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

serialize

v0.1.3

Published

A simple node utility to serialize execution of asynchronous functions

Downloads

586

Readme

Serialize

A simple node utility to serialize execution of asynchronous functions.

What does it do?

Asynchrny 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.

If you want to restore fs.writeFile and fs.mkdir to their original version, just do:

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

What if an error happens?

If an error happens, the error will be passed to the corresponding callback function, and the execution of all serialized calls after it will be aborted. If an error happens in a function call that does not have a callback, the error will be passed down to the first call that has a callback function. For example, if the fs.mkdir('new') call in the above code throws an error, because there's no callback attached to that call, the error will be passed down to the callback of fs.writeFile(...). And, of course, fs.mkdir('new/folder') and fs.writeFile(...) won't be executed because an error occurred before them.

What's more to it?

If you want to serialize calls to file system, and serialize calls to database, but allow calls to file system and calls to database to happen concurrently, how to do it? You can serialize different functions into different queues. Functions belong to the same queue will be executed in serial, but functions between different queues can run concurrently. To serialize a function to a queue other than the default queue, give a queue name as the second argument of serialize:

conn.query = serialize(conn.query, "db");

Is any function serializable?

Current version of serialize can only serialize a function that satisfies the following conditions:

  1. It accepts a callback function, and invokes the callback when it is done;
  2. If the callback function is optional, the second to last argument cannot be a 'Function' type;
  3. If an error occurs, it passed the error as the first argument to the callback, and the error must be an instance of Error.

Note: Future version of serialize will be able to serialize a function that emits end and error events.

License - MIT

Copyright (c) 2013 Chaoran Yang ([email protected])

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.