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

waitfor.it

v0.7.6

Published

Sequential programming for node.js, end of callback hell

Downloads

11

Readme

let wait = require('waitfor.it');

wait.for's little brother with laxFiber support

Sequential programming for node.js, end of callback hell.

Simple, straightforward abstraction over Fibers.

By using waitfor.it, you can call any nodejs standard async function in sequential/Sync mode, waiting for result data, without blocking node's event loop (thanks to fibers)

A nodejs standard async function is a function in which the last parameter is a callback: function(err,data)

Advantages:

  • Avoid callback hell / pyramid of doom
  • Simpler, sequential programming when required, without blocking node's event loop (thanks to fibers)
  • Simpler, try-catch exception programming. (default callback handler is: if (err) throw err; else return data)
  • No multi-threaded debugging nightmares, only one fiber running at a given time (thanks to fibers)
  • Can use any node-standard async function with callback(err,data) as last parameter.
  • Plays along with node programming style. Write your async functions with callback(err,data), but use them in sequential/SYNC mode when required.
  • Plays along with node cluster. You design for one thread/processor, then scale with cluster on multicores.

Install:

npm i --save waitfor.it

Proper Use:

You need to be in a Fiber to be able to use waitfor.it. The ideal place to launch a fiber is when a request arrives, to handle it:

var server = http.createServer(
  function(req, res){
    console.log('req!');
    wait.createFiber(handler,req,res); //handle in a fiber, keep node spinning
  }).listen(8000);

then,at function handler(req,res) and every function you call from there, you'll be able to use wait.for(ayncFn, arguments...);

Minimal running example

var wait = require('waitfor.it');

function anyStandardAsync(param, callback){
    setTimeout( function(){
                  callback(null,'hi '+param);
        }, 5000);
};

function  testFunction(){
    console.log('fiber start');
    var result = wait.for(anyStandardAsync,'test');
    console.log('function returned:', result);
    console.log('fiber end');
};

console.log('app start');
wait.launchFiber(testFunction);
console.log('after launch');

Generic Usage:

var wait=require('waitfor.it');

// launch a new fiber
wait.createFiber(my_sequential_function, arg, arg, ...);

// in a fiber.. We can wait for async functions
function my_sequential_function(arg,arg...){
    // call async_function(arg1), wait for result, return data
    var myObj = wait.for(async_function, arg1); 
    
    // call myObj.querydata(arg1,arg2), wait for result, return data
    var myObjData = wait.forMethod(myObj,'queryData', arg1, arg2);
    
    console.log(myObjData.toString());
}

##Notes on non-standard callbacks. e.g.: connection.query from mysql, database.prepare on node-sqlite3

waitfor.it expects standardized callbacks. A standardized callback always returns (err,data) in that order.

A solution for the sql.query method and other non-standard callbacks is to create a wrapper function standardizing the callback, e.g.:

 connection.prototype.q = function(sql, params, stdCallback){ 
             this.query(sql,params, function(err,rows,columns){ 
                                 return stdCallback(err,{rows:rows,columns:columns}); 
                         });
 }

usage:

try {
  var result = wait.forMethod(connection, "q", options.sql, options.params); 
  console.log(result.rows);
  console.log(result.columns);
} 
catch(err) {
   console.log(err);
}

##How does wait.launchFiber works?

wait.launchFiber(genFn,param1,param2) starts executing the function genFn as a fiber-generator until a "yield" (wait.for) is found, then wait.launchFiber executes the "yielded" value (a call to an async function), and links generator's "next" with the async callback(err,data), so when the async finishes and the callback is called, the fiber/generator "continues" after the var x =wait.for(...).