promise.cb

transform an async function to callback style

Usage no npm install needed!

<script type="module">
  import promiseCb from 'https://cdn.skypack.dev/promise.cb';
</script>

README

promise.cb

transform an async function to callback style

Build Status Coverage Status npm version npm downloads npm license

Install

npm i promise.cb --save

API

const pcb = require('promise.cb');
const cbFn = pcb(asyncFn);

Example

const pcb = require('promise.cb');

async function fn1 (){
  return 1
}

const fn2 = require('co').wrap(function*(){
  return 2;
});

function fn3(){
  return Promise.resolve(3);
}

const _fn1 = pcb(fn1);
const _fn2 = pcb(fn2);
const _fn3 = pcb(fn3);
const cb = (err, res) => {
  assert(!err);
  console.log(res);
};

_fn1(cb); // log 1
_fn2(cb); // log 2
_fn3(cb); // log 3

Changelog

CHANGELOG.md

See Also

Why

  • callbackify looks good, but it's using asyncFn.length, so it's not working when using co.wrap
  • catch(e => cb(e)) is not right, if cb(e) throws, it will only cause the promise reject, not throw error up

License

the MIT License http://magicdawn.mit-license.org