Comparing version 1.0.0 to 2.0.0
38
index.js
'use strict' | ||
exports.array = function catering (callback) { | ||
exports.fromCallback = function (callback) { | ||
if (callback === undefined) { | ||
var promise = new Promise(function (resolve, reject) { | ||
callback = function (err, value) { | ||
callback = function (err, res) { | ||
if (err) reject(err) | ||
else resolve(value) | ||
else resolve(res) | ||
} | ||
}) | ||
} | ||
return [callback, promise] | ||
} | ||
exports.object = function catering (callback) { | ||
if (callback === undefined) { | ||
var promise = new Promise(function (resolve, reject) { | ||
callback = function (err, value) { | ||
if (err) reject(err) | ||
else resolve(value) | ||
} | ||
}) | ||
callback.promise = promise | ||
} | ||
return { callback, promise } | ||
return callback | ||
} | ||
exports.attach = function catering (callback) { | ||
if (callback === undefined) { | ||
const promise = new Promise(function (resolve, reject) { | ||
callback = function (err, value) { | ||
if (err) reject(err) | ||
else resolve(value) | ||
} | ||
}) | ||
exports.fromPromise = function (promise, callback) { | ||
if (callback === undefined) return promise | ||
callback.promise = promise | ||
} | ||
return callback | ||
promise | ||
.then(function (res) { process.nextTick(callback, null, res) }) | ||
.catch(function (err) { process.nextTick(callback, err) }) | ||
} |
{ | ||
"name": "catering", | ||
"version": "1.0.0", | ||
"description": "Cater to callback and promise crowds, internally use callbacks.", | ||
"version": "2.0.0", | ||
"description": "Simple utility to allow your module to be consumed with a callback or promise", | ||
"license": "MIT", | ||
@@ -15,4 +15,4 @@ "author": "Vincent Weevers", | ||
"devDependencies": { | ||
"standard": "~12.0.1", | ||
"tape": "~4.9.1" | ||
"standard": "^14.0.0", | ||
"tape": "^4.10.1" | ||
}, | ||
@@ -19,0 +19,0 @@ "keywords": [ |
# catering | ||
> **Cater to callback and promise crowds.** | ||
> Internally use callbacks, because you an old-school chef. | ||
> Simple utility to allow your module to be consumed with a callback or promise. | ||
@@ -15,45 +15,33 @@ [![npm status](http://img.shields.io/npm/v/catering.svg)](https://www.npmjs.org/package/catering) | ||
If your module internally uses callbacks: | ||
```js | ||
const catering = require('catering').array | ||
const { fromCallback } = require('catering') | ||
module.exports = function (maybeCallback) { | ||
const [callback, promise] = catering(maybeCallback) | ||
fs.readFile('example', callback) | ||
return promise | ||
module.exports = function (callback) { | ||
callback = fromCallback(callback) | ||
process.nextTick(callback, null, 'example') | ||
return callback.promise | ||
} | ||
``` | ||
```js | ||
const catering = require('catering').object | ||
If your module internally uses promises: | ||
module.exports = function (maybeCallback) { | ||
const { callback, promise } = catering(maybeCallback) | ||
fs.readFile('example', callback) | ||
return promise | ||
} | ||
``` | ||
```js | ||
const catering = require('catering').attach | ||
const { fromPromise } = require('catering') | ||
module.exports = function (maybeCallback) { | ||
const callback = catering(maybeCallback) | ||
fs.readFile('example', callback) | ||
return callback.promise | ||
module.exports = function (callback) { | ||
return fromPromise(Promise.resolve('example'), callback) | ||
} | ||
``` | ||
## Consume | ||
Either way your module can now be consumed in two ways: | ||
```js | ||
// Don't force promises on people. | ||
example((err, result) => ..) | ||
// Don't force callbacks on people. | ||
example((err, result) => {}) | ||
const result = await example() | ||
// Enjoy. | ||
example.then(result => ..) | ||
``` | ||
When converting from a promise to a callback, `fromPromise` calls the callback in a next tick to escape the promise chain and not let it steal your beautiful errors. | ||
## Install | ||
@@ -60,0 +48,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4153
19
57