Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/robojones/mk-promise
https://github.com/robojones/mk-promise
Last synced: 23 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/robojones/mk-promise
- Owner: robojones
- License: mit
- Created: 2017-01-05T20:03:29.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2017-01-09T15:31:12.000Z (almost 8 years ago)
- Last Synced: 2023-09-24T00:57:02.555Z (about 1 year ago)
- Language: JavaScript
- Size: 5.86 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# mk-promise - don't use callbacks - use promises
## Installation
`npm i mk-promise`
## Example - without error-handling
```javascript
// require mk-promise
const promify = require('mk-promise')// an example function with callback
function asyncFunction(value1, value2, callback) {
callback(value1 + value2, 'another value')
}// make a promise that resolves when the callback gets called
promify(asyncFunction, 10, 5).then(r => {
console.log(r[0] === 15) //true
})
``````javascript
// require mk-promise
const promify = require('mk-promise')// an example function with callback
function asyncFunction(value, callback) {if(typeof value === 'number') {
callback(value + 1)
} else {
//first argument of the callback is an error
callback(new Error('horrible things happened'))
}
}//make a promise that gets rejected
promify(asyncFunction, false).withError().catch(error => {
console.log(error) //horrible things happened
})//this promise gets resolved
promify(asyncFunction, 100).withError().then(r => {
console.log(r[0] === 101) //true
})
```## makePromise(asyncFn, ...args)
- __asyncFn__ \ - asynchronous function that needs a callback as last argument
- __...args__ \ - arguments for asyncFn__returns__ \
## Class: SpecialPromise
This class extends Promise. You can use the default '.then()' and '.catch()' methods.
### promise.withError()
__returns__ a \. If the first argument (converted to a Boolean) is true, the promise gets rejected with the first argument.