Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/apendua/promesify
A useful pattern to replace ugly callbacks with promises
https://github.com/apendua/promesify
Last synced: 9 days ago
JSON representation
A useful pattern to replace ugly callbacks with promises
- Host: GitHub
- URL: https://github.com/apendua/promesify
- Owner: apendua
- License: mit
- Created: 2014-08-25T17:52:04.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2014-11-28T23:29:05.000Z (almost 10 years ago)
- Last Synced: 2024-10-19T16:47:45.946Z (20 days ago)
- Language: JavaScript
- Homepage:
- Size: 176 KB
- Stars: 0
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# promesify [![Build Status](https://travis-ci.org/apendua/promesify.svg?branch=master)](https://travis-ci.org/apendua/promesify)
Suppose you have some asynchronous callback-based API, which you want to use but you like promises. This is where `promesify` becomes handy. So lets say you have some API object
```javascript
myApi = new SuperDuperApi();
```
Now instead of doing this:
```javascript
myApi.method1(function (err) {
if (err) return;
myApi.method2(function (err) {
if (err) return;
myApi.method3(function (err) {
// and so on ...
});
});
});
```
thanks to `promesify` you can be a little smarter:
```javascript
var Promesify = promesify({
methods: [ 'method1', 'method2', 'method3', /* ... */ ]
});
myApi = new Promesify(myApi);
myApi.method1().method2().method3().catch(function (err) {
console.log('something went wrong');
});
```