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

oknow

v1.0.0

Published

A simple promises module for deferred callbacks in JavaScript.

Downloads

8

Readme

Ok, now...

A simple promise module for deferred callbacks for JavaScript.

The idea

The idea behind this simple module was to allow chaining of functions much like how testing frameworks work - by explicitly mentioning when to move to next function using a keyword.

Install

npm install oknow

Assume

Assume we have 3 functions like below:

var readFromFile = function(ok) {
	//an async function that reads data from a file
	ok();
}
var updateUser = function(ok) {
	//some code here to update DB in async manner
	ok();
}
var deleteFile = function(ok) {
	//some code here to delete the file we used previously
	ok();
}

These are all individual functions that run async code inside them. The idea is to explicitly say ok when the function finishes, so the next function in the queue will execute if there was no error. Check the below section.

Basic Chaining

var oknow = require('oknow');
oknow(readFromFile)
.after(updateUser)
.after(deleteFile);

The readFromFile function would read a file's contents asynchronously, and when it finishes reading, it has to say ok. This is when the next function in the queue is called (updateUser and then deleteFile, one after the other).

Use Directly

readFromFile
.after(updateUser)
.after(deleteFile);
//notice that we don't need the 'oknow' reference
//functions have an 'after' function by default

When you require the module with require('oknow');, it overrides the Function prototype with an additional member called after that accepts another function. So you can directly call after on any function to execute it and chain it with the next.

Simple Error Handling

readFromFile
.after(function(ok) {
	//code to update user, error occurred!
	//Pass the error object to the 'ok' function
	if (err) {
		ok(err);
	} else {
		ok();
	}
})
.after(function(ok) {
	//code to delete file
	ok();
})
.catch(function(err) {
	console.log('Error: ' + err.message);
});
//notice that the delete file function is not called due to error
//control moves to the catch block

When calling ok from within any async function, pass an error object to it as parameter - this will break the execution chain and jump directly to the available catch block.

Passing Data

The module supports passing data from one call to another, for dependable functions that need the result from previously called function. This is achieved by passing the parameters directly to the ok call, and reading them in the next function as arguments. (Remember, the first argument is always ok!).

Example

readFromFile
.after(function(ok) {
    var theFileContents = 'User = John, Age = 25'; //read file contents into a variable
    ok(theFileContents);
})
.after(function(ok, theFileContents) {
	//update data based on file content
	console.log(theFileContents); //User = John, Age = 25
	ok();
})
.after(function() {
	console.log("Done");
});

Contributing

OkNow is created by @ritenv. Contributions are open and welcome. For any issues, please raise it in the issues section and feel free to send pull requests to fix them.