awaitable callback.

Usage no npm install needed!

<script type="module">
  import superjsCb from 'https://cdn.skypack.dev/@superjs/cb';



awaitable callback.

async/await way to call callback style functions


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


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

  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)



  • 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


  • returns: cb, a Promise

Pop the top cb in cbStack and return it


  • 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()


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


  • value: value to be resolved with

resolve the Promise cb with value


  • err: error to be rejected with

reject the Promise cb with err


  • ...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')

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

function foo(a,b,cb){


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