Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/zbinlin/promise-adapter
Convert node-style callback to promise
https://github.com/zbinlin/promise-adapter
Last synced: 4 days ago
JSON representation
Convert node-style callback to promise
- Host: GitHub
- URL: https://github.com/zbinlin/promise-adapter
- Owner: zbinlin
- Created: 2015-08-10T03:07:36.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2015-11-10T16:38:20.000Z (about 9 years ago)
- Last Synced: 2024-08-08T21:52:41.773Z (3 months ago)
- Language: JavaScript
- Size: 133 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# promise-adapter
Convert node-style callback to promise
## Install
```shell
npm install promise-adapter
```## Usage
### Example 1
```js
var promising = require("promise-adapter");
function sqrtAsync(n, cb) {
process.nextTick(function () {
cb(n * n);
});
}
var sqrt = promising(sqrtAsync);
sqrt(10).then(function (val) {
var data = val[0];
console.log(data); // 100
}, function (err) {
console.error(err);
});
```### Example 2
```js
var fs = require("fs");
var promising = require("promise-adapter");var readFile = promising(fs.readFile.bind(fs));
readFile("index.js").then(function (val) {
var data = val[0];
console.log(data);
}, function (err) {
console.error(err);
});
```### Example 3
```js
var promising = require("promise-adapter");var hello = function (cb) {
var that = this;
process.nextTick(function () {
cb("Hello, My name is " + that.name + "!");
});
};var jim = {
name: "Jim"
};
var kate = {
name: "Kate"
};var jimSayHello = promising(hello, jim);
var kateSayHello = promising(hello, kate);jimSayHello().then(function (val) {
var data = val[0];
console.log(data); // Hello, My name is Jim!
}, function (err) {
console.error(err);
});kateSayHello().then(function (val) {
var data = val[0];
console.log(data); // Hello, My name is Kate!
}, function (err) {
console.error(err);
});
```### Example 4
```js
var promising = require("promise-adapter");var addSync = function (a, b) {
return a + b;
}var add = promising(addSync, true);
add(1, 2).then(function (val) {
var data = val[0];
console.log(data); // 3
}, function (err) {
console.error(err);
});
```