Socket
Socket
Sign inDemoInstall

relike

Package Overview
Dependencies
35
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    relike

Simple promisify a callback-style function with sane defaults. Support promisify-ing sync functions.


Version published
Weekly downloads
3
Maintainers
1
Created
Weekly downloads
 

Changelog

Source

1.0.3 - 2016-01-26

  • Release v1.0.3 / npm@v1.0.3
  • add .promisify method

Readme

Source

relike npmjs.com The MIT License

Simple promisify a callback-style function with sane defaults. Support promisify-ing sync functions.

code climate standard code style travis build status coverage status dependency status

What's the difference?

What's the difference between me and you?!
–– Dr Dre feat. Eminem & X-Zibit - Whats the Difference, https://youtu.be/8y5MjguI-pM

What's the difference between this module, relike-value and redolent?
–– Simply, almost nothing.

  1. This one just accept sync or async function which is executed immediately with next arguments, after that it returns Promise.
  2. relike-value accepts everything and returns Promise. But it is little bit tricky:
  • if you pass only one non-function argument to it, it will pass it to promise
  • if you pass more that one arguments to it, it will create array from them and pass it to promise
  • if you pass function as first argument, next arguments will be passed to this function
  1. redolent accepts everything and returns function, which when is executed it returns Promise. Above things applies here, because it is on top of relike-value.

Notice: Both relike and relike-value direcly executes first argument (if function) and returns Promise.

Install

npm i relike --save

Usage

For more use-cases see the tests

const relike = require('relike')

relike

Will try to promisify fn with native Promise, otherwise will use Bluebird or you can give different promise module to relike.promise, for example pinkie.

  • <fn> {Function} callback-style or synchronous function to promisify
  • return {Promise} promise

Example

const fs = require('fs')
const request = require('request')
const relike = require('relike')

relike(fs.readFile, 'package.json', 'utf-8').then(data => {
  console.log(JSON.parse(data).name)
})

// promisify sync function
relike(fs.readFileSync, 'package.json', 'utf-8')
.then(JSON.parse)
.then(res => {
  console.log(res.name)
})

// handles multiple arguments by default (comes from `request`)
relike(request, 'http://www.tunnckocore.tk/').then(result => {
  const [httpResponse, body] = result
})

relike.promisify

Wraps a function and returns a function that when is invoked returns Promise.
Same as Bluebird.promisify or any other "promisify" thing - accept function and return a function.

  • <fn> {Function} callback-style or synchronous function to promisify
  • [Prome] {Function} custom Promise constructor/module to use, e.g. Q
  • return {Function} promisified function

Example

const fs = require('fs')
const relike = require('relike')
const readFile = relike.promisify(fs.readFile)

readFile('package.json', 'utf8')
  .then(JSON.parse)
  .then(data => {
    console.log(data.name) // => 'relike'
  })

relike.promise

Static property on which you can pass custom Promise module to use, e.g. Q constructor.

Example

const fs = require('fs')
const relike = require('relike')

// `q` promise will be used if not native promise available
// but only in node <= 0.11.12
relike.promise = require('q')
relike(fs.readFile, 'package.json', 'utf-8').then(data => {
  console.log(JSON.parse(data).name)
})

Access Promise constructor

You can access the used Promise constructor for promisify-ing from promise.Prome

Example

const fs = require('fs')
const relike = require('relike')

// use `pinkie` promise if not native promise available
// but only in node <= 0.11.12
relike.promise = require('pinkie')

const promise = relike(fs.readFile, 'package.json', 'utf8')

console.log(promise.Prome)
//=> will be `pinkie` promise constructor (only in node <= 0.11.12)
console.log(promise.Prome.___customPromise) //=> true (only on node <= 0.11.12)
console.log(promise.___customPromise) //=> true (only on node <= 0.11.12)

promise
  .then(JSON.parse)
  .then(data => {
    console.log(data.name) //=> `relike`
  })
  • always-done: Handles completion and errors of anything!
  • always-promise: Promisify, basically, everything. Generator function, callback-style or synchronous function; sync function that returns child process, stream or observable; directly passed promise, stream or child process.
  • always-thunk: Thunkify, basically, everything. Generator function, callback-style or synchronous function; sync function that returns child process, stream or observable; directly passed promise, stream or child process.
  • always-generator: Generatorify, basically, everything. Async, callback-style or synchronous function; sync function that returns child process, stream or observable; directly passed promise, stream or child process.
  • native-or-another: Always will expose native Promise if available, otherwise Bluebird but only if you don't give another promise module like q or promise or what you want.
  • native-promise: Get native Promise or falsey value if not available.
  • redolent: Simple promisify everything (string, array, stream, boolean, sync/async function, etc) with sane defaults.
  • relike-all: Promisify all functions in an object, using relike.
  • relike-value: Create promise from sync, async, string, number, array and so on. Handle completion (results) and errors gracefully! Built on top of relike, used by redolent to build robust (hybrid) APIs.

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
But before doing anything, please read the CONTRIBUTING.md guidelines.

Charlike Make Reagent new message to charlike freenode #charlike

tunnckocore.tk keybase tunnckocore tunnckoCore npm tunnckoCore twitter tunnckoCore github

Keywords

FAQs

Last updated on 26 Jan 2016

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc