Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/elldritch/promisebacker
A callback-to-promise-and-back adapter.
https://github.com/elldritch/promisebacker
Last synced: 5 days ago
JSON representation
A callback-to-promise-and-back adapter.
- Host: GitHub
- URL: https://github.com/elldritch/promisebacker
- Owner: elldritch
- License: mit
- Created: 2014-12-30T10:47:15.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2014-12-31T06:50:37.000Z (almost 10 years ago)
- Last Synced: 2024-09-27T21:07:41.481Z (about 2 months ago)
- Language: CoffeeScript
- Size: 129 KB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Promisebacker
Wraps an asynchronous function that either takes a callback or returns a promise, and allows it to do both.**Note:** Promisebacker is still under development and not yet ready for production systems. Releases are stable but the API is subject to rapidly change.
## Installation
`npm install promisebacker`## Example
```js
var Promise = require('promise') // Any Promises/A+ compliant library will do.
, promisebacker = require('promisebacker');var takes_callback = function(arg1, arg2, callback) {
// Does something...
var error = false;// Asynchronously invokes callback.
setTimeout(function() {
if (error){
return callback('uh oh!');
}
callback(null, 'hello world!');
}, 3000);
};var returns_promise = function(arg1, arg2) {
// Does something...
var error = false;// Returns promise that resolves asynchronously.
return new Promise(function(resolve, reject) {
setTimeout(function() {
if (error) {
return reject('uh oh!');
}
resolve('hello world!');
}, 3000);
});
};var wrapped_callback = promisebacker(takes_callback);
// Now we can pretend this returns a promise...
wrapped_callback('alas', 'poor yorick')
.then(function(result) {
// result == 'hello world!'
});// ...or we can continue using it as a callback!
wrapped_callback('alas', 'poor yorick', function(err, result) {
// err == null
// result == 'hello world!'
});// And we can do the same for functions that return promises.
var wrapped_promise = promisebacker(returns_promise);wrapped_promise('alas', 'poor yorick')
.then(function(result) {
// result == 'hello world!'
});wrapped_promise('alas', 'poor yorick', function(err, result) {
// err == null
// result == 'hello world!'
});```
## Usage Notes
`Promisebacker(Function target)` assumes that you're trying to use a callback if the last argument passed is a `Function` of arity at least 2. If you want to force it to return promises, use `Promisebacker.toPromise` instead.## API Reference
We define a function to take a _node-style callback_ (a _nodeback_) if it accepts a `Function` of arity at least 2 as its last argument and invokes that function whenever it finishes running. When invoking its callback, it must pass an error value as its first argument which must be truthy if and only if an error has occurred.##### `Promisebacker(Function target [, Object options])` -> `Function`
Wraps `target` such that it can either take a callback or return a promise.
* `target` must either return a promise or take nodebacks.
* `options` is an optional object with options.If `target` takes nodebacks and calls its callback with multiple success values, the fulfillment value will be an array of them.
See the `bluebird` documentation for [promisification](https://github.com/petkaantonov/bluebird/blob/master/API.md#promisification) for details.
###### Option: `Object scope` (default: N/A)
If you pass a `scope`, then `target` will have its `this` bound to `scope` (i.e. as if it were being called as `scope.target`).###### Option: `Boolean spread` (default: `false` if nodeback is of arity at most 2, `true` otherwise)
Some nodebacks expect more than 1 success value but there is no mapping for this in the promise world. If `spread` is specified, the nodeback is called with multiple values when the promise fulfillment value is an array:```js
var example = Promisebacker(Promise.resolve);
example([1, 2, 3], function(err, result) {
// err == null
// result == [1, 2, 3]
});var another = Promisebacker(Promise.resolve, {spread: true});
another([1, 2, 3], function(err, a, b, c) {
// err == null
// a == 1, b == 2, c == 3
});
```##### `Promisebacker.toPromise(Function target [, Object options])` -> `Function`
Same as above, but will always return a `Promise` even if the last argument is a `Function` of arity at least 2.## License
© 2014 Lehao Zhang. Released under the terms of the MIT license.