proback

1.0.3 • Public • Published

proback

NPM version Build Status Dependency Status Coverage percentage

A utility to create a promise callback

Installation

$ npm install --save proback

Usage

Generic usage

var proback = require('proback');
 
function fn(throws, cb) {
  if (typeof throws === 'function') {
    cb = throws;
    throws = null;
  }
  cb = cb || proback();
  setTimeout(function () {
    if (throws) {
      cb(throws);
    } else {
      cb();
    }
  });
  return cb.promise;
}
 
fn(function () {
  console.log('callback done', arguments);    // callback done {}
});
 
fn('abc', function () {
  console.log('callback done', arguments);    // callback done { '0': 'abc' }
});
 
fn().then(function () {
  console.log('promise resolved', arguments); // promise resolved { '0': undefined }
});
 
fn('def').catch(function () {
  console.log('promise reject', arguments);   //promise reject { '0': 'def' }
});
 

Resolve with multiple arguments

function fnResolveWithMultiArgs(cb) {
  cb = cb || proback();
  setTimeout(function () {
    cb(null, 1, 2, 3);
  });
  return cb.promise;
}
 
fnResolveWithMultiArgs().then(function (data) {
  console.log(data);    // [1, 2, 3]
});
    
fnResolveWithMultiArgs().spread(function (a, b, c) {
  console.log(a, b, c); // 1, 2, 3
});

License

MIT © taoyuan

Readme

Keywords

Package Sidebar

Install

npm i proback

Weekly Downloads

12

Version

1.0.3

License

MIT

Last publish

Collaborators

  • bitt