Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/oliverjash/nodeify-function
Q helper for nodeifying a promise returning function.
https://github.com/oliverjash/nodeify-function
Last synced: 25 days ago
JSON representation
Q helper for nodeifying a promise returning function.
- Host: GitHub
- URL: https://github.com/oliverjash/nodeify-function
- Owner: OliverJAsh
- Created: 2014-11-24T18:44:14.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2015-01-08T15:44:34.000Z (almost 10 years ago)
- Last Synced: 2024-04-14T09:43:59.074Z (7 months ago)
- Language: JavaScript
- Homepage:
- Size: 164 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# nodeify-function
Q helper for nodeifying a promise returning function.
This method is useful for creating dual promise/callback APIs, i.e. APIs that
return promises but also accept Node.js-style callbacks. [Q provides a helper
for nodeifying promises:
`promise.nodeify(callback)`](https://github.com/kriskowal/q/wiki/API-Reference#promisenodeifycallback).
This function is the inverse: rather than nodeifying the promise, we can simply
nodeify the function containing the promise. For example:``` js
var createUser = function (userName, userData) {
return database.ensureUserNameNotTaken(userName)
.then(function () {
return database.saveUserData(userName, userData);
});
}createUser('Bob', { age: 42 })
.then(function (result) {
// …
}, function (error) {
// …
});// Without `nodeifyFunction`
var createUserNodeified = function (userName, userData, callback) {
createUser(userName, userData)
.nodeify(callback);
};// With `nodeifyFunction`: a Q helper that abstracts the above into a
// higher-order functionvar nodeifyFunction = require('nodeify-function');
var createUserNodeified = nodeifyFunction(createUser);
createUserNodified('Bob', { age: 42 }, function (error, result) {
// …
});
```## Installation
```
npm install nodeify-function
```