![Introducing Enhanced Alert Actions and Triage Functionality](https://cdn.sanity.io/images/cgdhsj6q/production/fe71306d515f85de6139b46745ea7180362324f0-2530x946.png?w=800&fit=max&auto=format)
Product
Introducing Enhanced Alert Actions and Triage Functionality
Socket now supports four distinct alert actions instead of the previous two, and alert triaging allows users to override the actions taken for all individual alerts.
relike
Advanced tools
Changelog
1.1.4 - 2016-05-16
Readme
Simple promisify async or sync function with sane defaults. Lower level than
promisify
thing. Can be used to createpromisify
method.
npm i relike --save
For more use-cases see the tests
const relike = require('relike')
Note: It treat functions as asynchronous, based on is-async-function.
Why you should be aware of that? Because if you give async function which don't have last argument called with some of the common-callback-names it will treat that function as synchronous and things may not work as expected.
It's not a problem for most of the cases and for node's native packages, because that's a convention.
So, the relike-all package successfuly can promisifies all of the fs
functions for example, except fs.createReadStream
and fs.createWriteStream
which is normal.
Runs
fn
in native Promise if available, or another provided inrelike.Promise
. If not given and not support for native Promise, it will use bluebird promise, but only on that enviroments that don't have support.
Params
<fn>
{Function}: Some async or synchronous function.[...args]
{Mixed}: Any number of any type of arguments, they are passed to fn
.returns
{Promise}: Always native Promise if supported on enviroment.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) // => 'relike'
})
// handles multiple arguments by default (comes from `request`)
relike(request, 'http://www.tunnckocore.tk/').then(result => {
const [httpResponse, body] = result
})
Thin wrapper function around
relike()
. It accepts a function and returns a function, which when is invoked returns aPromise
. Just like any other.promisify
method, for exampleBluebird.promisify
.
Params
fn
{Function}: Some sync or async function to promisify.[Promize]
{Function}: Promise constructor to be used on enviroment where no support for native.returns
{Function}: Promisified function, which always return a Promise.Example
var fs = require('fs')
var relike = require('relike')
var readFile = relike.promisify(fs.readFile)
readFile('package.json', 'utf8')
.then(JSON.parse)
.then(data => {
console.log(data.name) // => 'relike'
})
// also can promisify sync function
var statFile = relike.promisify(fs.statSync)
statFile('package.json')
.then(function (stats) {
console.log(stats.mtime)
})
While relike
always trying to use native Promise if available in the enviroment, you can
give a Promise constructor to be used on enviroment where there's no support - for example, old
broswers or node's 0.10 version. By default, relike
will use and include bluebird on old enviroments,
as it is the fastest implementation of Promises. So, you are able to give Promise constructor, but
it won't be used in modern enviroments - it always will use native Promise, you can't trick that. You
can't give custom promise implementation to be used in any enviroment.
Example
var fs = require('fs')
var relike = require('relike')
relike.promisify.Promise = require('q') // using `Q` promise on node 0.10
var readFile = relike.promisify(fs.readFile)
readFile('package.json', 'utf8')
.then(console.log, console.error)
One way to pass a custom Promise constructor is as shown above. But the other way is passing it to .Promise
of the promisified function, like that
var fs = require('fs')
var relike = require('relike')
var statFile = relike.promisify(fs.stat)
statFile.Promise = require('when') // using `when` promise on node 0.10
statFile('package.json').then(console.log, console.error)
One more thing, is that you can access the used Promise and can detect what promise is used. It is easy, just as promise.Promise
and you'll get it.
Or look for promise.___bluebirdPromise
and promise.___customPromise
properties. .___bluebirdPromise
(yea, with three underscores in front) will be true if enviroment is old and you didn't provide promise constructor to .Promise
.
So, when you give constructor .__customPromise
will be true and .___bluebirdPromise
will be false.
var fs = require('fs')
var relike = require('relike')
var promise = relike(fs.readFile, 'package.json', 'utf8')
promise.then(JSON.parse).then(function (val) {
console.log(val.name) // => 'relike'
}, console.error)
console.log(promise.Promise) // => used Promise constructor
console.log(promise.___bluebirdPromise) // => `true` on old env, falsey otherwise
console.log(promise.___customPromise) // => `true` when pass `.Promise`, falsey otherwise
Or finally, you can pass Promise constructor as second argument to .promisify
method. Like that
const fs = require('fs')
const relike = require('relike')
const readFile = relike.promisify(fs.readFile, require('when'))
const promise = readFile('index.js')
console.log(promise.Promise) // => The `when` promise constructor, on old enviroments
console.log(promise.___customPromise) // => `true` on old environments
relike
. | homepagePull 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.
FAQs
Simple promisify async or sync function with sane defaults. Lower level than `promisify` thing. Can be used to create `promisify` method.
The npm package relike receives a total of 3 weekly downloads. As such, relike popularity was classified as not popular.
We found that relike demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
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.
Product
Socket now supports four distinct alert actions instead of the previous two, and alert triaging allows users to override the actions taken for all individual alerts.
Security News
Polyfill.io has been serving malware for months via its CDN, after the project's open source maintainer sold the service to a company based in China.
Security News
OpenSSF is warning open source maintainers to stay vigilant against reputation farming on GitHub, where users artificially inflate their status by manipulating interactions on closed issues and PRs.