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

@superjs/cb

v1.0.2

Published

awaitable callback

Downloads

5

Readme

@superjs/cb

awaitable callback.

async/await way to call callback style functions

Thought

If you want to convert callback style to async/await style, you can either:

  1. wrap the operation to return a new Promise, which is ugly
  2. or, just call the operation, using @superjs/cb as callback, then await the cb

Example

const Cb = require('@superjs/cb')
// await can't be used without being wrapped by async function
;(async ()=>{
  // Cb() create a plain Promise
  // with some extra fields/magics
  setTimeout(Cb().ok,2000)

  await Cb.pop()
})()

You can also use it as event handlers:

const Cb = require('@superjs/cb')
const {spawn} = require('child_process')
// await can't be used without being wrapped by async function
;(async ()=>{
  let proc = spawn('ls')
  // Cb().ok resolve 1st arg
  proc.stdout.on('data', Cb().ok) 
  // use Cb instead of Cb() to reference the last created one
  proc.on('error', Cb.err) //Cb.err reject 1st arg
  try {
    let out = await Cb.pop()
    console.log(`out: `+ out)
  }
  catch (err) {
    console.log(`err: `, err)
  }
})()

API

Cb()

  • returns: Cb, return itself

calling Cb will create a Promise: cb, then push cb to cbStack. From now on, Cb will ref to the cb created

ref means you can use Cb.ok/err/arr/pair to get cb.ok/err/arr/pair

Cb.pop()

  • returns: cb, a Promise

Pop the top cb in cbStack and return it

Cb.new()

  • returns: cb, a Promise

return a standalone cb not included by cbStack

comparing to Cb()/Cb.pop(), you would have to add a local variable to reference to Cb.new()

cb.then/catch/finally

since cb is a Promise, use them just like new Promise(...).then/catch/finally

cb.ok(value)

  • value: value to be resolved with

resolve the Promise cb with value

cb.err(err)

  • err: error to be rejected with

reject the Promise cb with err

cb.arr(...args)

  • ...args: arg list to be resolved with

collect args received as an array and resolve with args

cb.pair(err, value)

  • err: error to be rejected with, err==null to resolve value

  • value: value to be resolved with, if err==null

Use together with @superjs/wait

@superjs/wait can await something synchronously.

const Cb = require('@superjs/cb')
const wait = require('@superjs/wait')

foo(1,2,Cb().arr) 
// wait instead of await
let result = wait(Cb.pop()) 
// print [3,-1] one second later
console.log(result) 

function foo(a,b,cb){
  setTimeout(()=>cb(a+b,a-b),1000)
}

Compatibility

You can use @superjs/cb in both nodejs and browser environment.