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

cb-async

v1.0.2

Published

Creates an async/await/promise style callback handler with a single function call.

Downloads

7

Readme

CB Async: Callback Async

A compact function for handling callbacks as Promises. Just create the cb-async handler, pass it as your callback, and await the results.

No Nesting! No Wrapping! Just a handle to the callback!

Includes multiple builds, Typescript files is included for custom builds (see builds below). Examples are in ES2017. Module requires at least ES6 to run. If you need to stay backwards compatible use TypeScript or Babel. No excuses anymore!

Installation

npm -i cb-async

Basic Usage

//ES6 Module:
import CBAsync from './cb-async/index.js'
//Node/CommonJS: let CBAsync = require('cb-async')

//Function that does something and tells you when its done
async function doSomething(onCompleted){
	//something happens here
	onCompleted(response);
}

//Create an async callback handler 
let completed = CBAsync();

doSomething(completed);

//Wait on the handler to finish 
let myResponse = await completed; 

Real Example: (ES 2017)

//Example using the facebook javascript sdk
import CBAsync from './cb-async/index.js'
//cb-async : Simple 16 Lines, 1 Try/Catch Clear Resolution Path
async function checkFBLogin() {
    let loggedIn = false;
    try {
        let loginCallback = CBAsync();
        FB.getLoginStatus(loginCallback);
        let response = await loginCallback;
        if (response.status === 'connected') {
            let meCallback = CBAsync();
            FB.api("/me?fields=email",meCallback);
            let responseMe = await meCallback;
            loggedIn = true;
        }
    }
    catch (err) {
        console.error(err.message);
    }
    return loggedIn;
}
//No cb-async: Callback hell. 26 lines, 3 Try/Catch , Multiple Resolution paths 
function checkFBLogin() {
    return new Promise(function(resolve,reject){
		try{
			FB.getLoginStatus(function(response){
				try{
					if (response.status === 'connected') {
			            FB.api("/me?fields=email",function(responseMe){
							try{				            
								//Do something
							    resolve(true);
							}
							catch(err){
								reject(err);
								console.log(err);
							}
			            });   
			        }
			        resolve(false)
				} catch(err){
					reject(err);
					console.error(err.message)
				}
			});
		} catch(err){
			reject(err);
			console.error(err.message)
		}
	});

Documentation

function CBAsync(handler)

Parameters:

handler (optional)

When provided, this function is executed prior to resolving awaiter/promise. This can be an async function/promise. In which case it will be awaited. The 'this' value is set according the the callback, and all of the callback parameters can be accessed in this function. This is primarily for readability or to potentially branch to a set of different tasks independent of the result thread. See example below.

Returns: Proxy

The function returns a 'PromiseLike' proxy that can be provided as a callback function and can be awaited. The result of the promise is either an value ( 1 callback parameter), array (multiple callback parameters).

Multiple Callback Parameters & Pre-Resolution Handler

When there is only 1 parameter then it is passed back from the await/promise. If there are multiple parameters then an array is passed back. The contents of the array are the parameters in the order they would have been passed to a standard call back.

Internal:

/** 
*** This is an example of using cb-async as a callback with tedious.
*** Note how the pre-resolution handler is used to close the connection. (Primarily for readability)
**/


   let connection: any
   connection = await this.connect(config);
   
   //Create the callback and immediately code that I want to close the connection when it is done.
   //Also set my variables using the typical callback style;
   let err, rows
   let sqlCB = CBAsync((_err, _count, _rows) =>{
   		connection.close();
   		
   		//Its typically better to just use the results of the cb-async promise. (See below)
   		err = _err;
   		rows = _rows;
   		
   });
   
   //Create the request and pass the asynchronous callback
   let sqlReq = new Request(proc, sqlCB);
   sqlReq.addParameter(input.name, TYPES.NVarChar, JSON.stringify(input.value));
   
   //Call the proc
   connection.callProcedure(sqlReq);
   
   //Get the results 
   //Note: When there is only 1 paramter it is returned, not an array
   let sqlResults = await sqlCB;

   console.log(results[0] === err) //true
   console.log(results[2] === row) //true		


   err = sqlResults[0];
   if (err) throw err;
   rows = sqlResults[2];



   	
 

Builds

Below are the current builds. The TypeScript file used is included. So feel free to make any custom build that you need. Just note that Proxy is required.

| (index.js) | Module | Target| Notes | ------------- | :-------------: |:--------:|----- | ./ | CommonJS | ES6| Node 8+ | ./src/ES2017 |ES6 | ES2017 | async/await (native promises) | ./src/ES6| ES6| ES6 | Web Legacy