Skip to content

Make asynchronous methods support both Promise and callback.

License

Notifications You must be signed in to change notification settings

morenyang/create-promise-callback

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

create-promise-callback

NPM version Build Status

Make asynchronous methods support both Promise and callback.

Install

$ yarn add create-promise-callback

Usage

import createPromiseCallback from 'create-promise-callback';

function get(key, fn) {
  fn = fn || createPromiseCallback();

  RedisClient
    .get(key)
    .then((err, res) => {
      if(err) return fn(err);
      fn(null, res);
    })

  return fn.promise;
}

// callback
get('yourkey', (err, res) => {
  console.log(res);
})

// promise
get('yourkey')
  .then(res => {
    console.log(res);
  })
  .catch(err => {
    console.log(err);
  })

// also resolve with multi arguments
function multiArgsFunc(fn) {
  fn = fn || createPromiseCallback();

  yourFunc()
    .then((err, arg1, arg2) => {
      if(err) return fn(err);
      fn(null, arg1, arg2);
    })

  return fn.promise;
}

multiArgsFunc()
  .then((arg1, arg2) => {
    // ...
  })

Use your preferred Promise library

set global.Promise to your preferred Promise libaray like bluebird.

global.Promise = require('bluebird')

License

MIT

About

Make asynchronous methods support both Promise and callback.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published