Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dimensionsoftware/optional-callback
Allow Promise-returning functions to also take callbacks
https://github.com/dimensionsoftware/optional-callback
Last synced: about 1 month ago
JSON representation
Allow Promise-returning functions to also take callbacks
- Host: GitHub
- URL: https://github.com/dimensionsoftware/optional-callback
- Owner: DimensionSoftware
- License: mit
- Created: 2016-09-23T19:35:21.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2016-10-26T00:23:10.000Z (about 8 years ago)
- Last Synced: 2024-11-09T00:55:39.403Z (about 2 months ago)
- Language: JavaScript
- Homepage:
- Size: 20.5 KB
- Stars: 2
- Watchers: 5
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# optional-callback(fn) : Function
[![npm version](https://badge.fury.io/js/optional-callback.svg)](https://badge.fury.io/js/optional-callback)
[![Build Status](https://travis-ci.org/DimensionSoftware/optional-callback.svg?branch=master)](https://travis-ci.org/DimensionSoftware/optional-callback)
[![Documentation Status](https://readthedocs.org/projects/optional-callback/badge/?version=latest)](http://optional-callback.readthedocs.io/en/latest/?badge=latest)A function that turns a Promise-returning function into a function that may also take an optional callback as the last parameter instead
## Install
```sh
npm install --save optional-callback
```## Example
```javascript
var Promise = require('bluebird');
var oc = require('optional-callback');/* This is a simple function that returns a Promise that will resolve after
the time specified in `delay` passes.@param {Number} delay milliseconds to wait
@return {Promise} a Promise that will resolve after the `delay` milliseconds passes
*/
function foo(delay) {
return Promise.delay(delay);
}/* This function works just like `foo`, but it can also take an optional callback.
@param {Number} delay milliseconds to wait
@param {Function} cb (optional) if provided, the callback will be run after the `delay` expires
@return {Promise?} If `cb` is not passed, a Promise will be returned.
Otherwise, nothing will be returned.
*/
var foo2 = oc(foo);// foo2 can work just like the original foo with Promises
foo2(1000).then(function() {
console.log('done');
}).catch(function(err){
console.warn(err);
});// foo2 may also be called with a callback
foo2(1000, function(err){
if (err) return console.warn(err);
console.log('done');
});
```