Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

relike

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

relike - npm Package Compare versions

Comparing version 0.0.0 to 1.0.0

utils.js

6

CHANGELOG.md
## 1.0.0 - 2016-01-15
- Release v1.0.0 / npm@v1.0.0
- docs
- update duplication
- implement :star:
## 0.0.0 - 2016-01-15
- Initial commit

63

index.js

@@ -10,4 +10,63 @@ /*!

module.exports = function relike () {
// body
var utils = require('./utils')
/**
* 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`.
*
* **Example**
*
* ```js
* 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)
* })
*
* // handles multiple arguments by default (comes from `request`)
* relike(request, 'http://www.tunnckocore.tk/').then(result => {
* const [httpResponse, body] = result
* })
* ```
*
* @name relike
* @param {Function} `<fn>` callback-style or synchronous function to promisify
* @return {Promise} promise
* @api public
*/
module.exports = function relike (fn) {
var Prome = utils.nativeOrAnother(relike.promise)
if (typeof fn !== 'function') {
return Prome.reject(new TypeError('relike expect a function'))
}
var argz = utils.handleArguments(arguments)
var self = this
argz.args = argz.args.slice(1)
if (argz.callback && !utils.isAsyncFunction(argz.callback)) {
argz.args = argz.args.concat(argz.callback)
}
var promise = new Prome(function prome (resolve, reject) {
var isAsync = utils.isAsyncFunction(fn)
if (isAsync) {
argz.args = argz.args.concat(function cb (err, res) {
if (err) return reject(err)
if (arguments.length > 2) res = utils.sliced(arguments, 1)
resolve(res)
})
}
var syncResult = fn.apply(self, argz.args)
if (!isAsync) {
resolve(syncResult)
}
})
promise.Prome = Prome
promise.___customPromise = Prome.___customPromise
promise.___bluebirdPromise = Prome.___bluebirdPromise
return promise
}
{
"name": "relike",
"version": "0.0.0",
"version": "1.0.0",
"description": "Simple promisify a callback-style function with sane defaults. Support promisify-ing sync functions.",

@@ -12,12 +12,32 @@ "repository": "hybridables/relike",

},
"dependencies": {},
"dependencies": {
"handle-arguments": "^3.0.4",
"is-async-function": "^1.1.0",
"lazy-cache": "^1.0.3",
"native-or-another": "^3.0.1",
"sliced": "^1.0.1"
},
"devDependencies": {
"assertit": "^0.1.0"
"assertit": "^0.1.0",
"is-buffer": "^1.1.0",
"isarray": "1.0.0",
"pinkie": "^2.0.1",
"semver": "^5.0.3"
},
"files": [
"index.js"
"index.js",
"utils.js"
],
"keywords": [
"relike"
"anything",
"bluebird",
"callback",
"defaults",
"everywhere",
"native",
"native-promise",
"promise",
"promisify",
"simple"
]
}

@@ -21,3 +21,87 @@ # [relike][author-www-url] [![npmjs.com][npmjs-img]][npmjs-url] [![The MIT License][license-img]][license-url]

### [relike](./index.js#L38)
> 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**
```js
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.promise
> Static property on which you can pass custom Promise module to use, e.g. `Q` constructor.
**Example**
```js
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**
```js
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`
})
```
## Related
- [always-done](https://github.com/hybridables/always-done): Handles completion and errors of anything!
- [always-promise](https://github.com/hybridables/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](https://github.com/hybridables/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](https://github.com/hybridables/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](https://github.com/tunnckoCore/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](https://github.com/tunnckoCore/native-promise): Get native `Promise` or falsey value if not available.
## Contributing

@@ -24,0 +108,0 @@ Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/hybridables/relike/issues/new).

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc