smartpromise/README.md

85 lines
3.3 KiB
Markdown
Raw Normal View History

2018-07-03 06:39:25 +00:00
# @pushrocks/smartpromise
2018-03-16 09:24:12 +00:00
2018-07-03 06:39:25 +00:00
smart helpers that work with promises
2016-12-16 20:19:01 +00:00
## Availabililty
2018-03-16 09:24:12 +00:00
2017-02-12 11:56:22 +00:00
[![npm](https://pushrocks.gitlab.io/assets/repo-button-npm.svg)](https://www.npmjs.com/package/smartq)
[![git](https://pushrocks.gitlab.io/assets/repo-button-git.svg)](https://GitLab.com/pushrocks/smartq)
[![git](https://pushrocks.gitlab.io/assets/repo-button-mirror.svg)](https://github.com/pushrocks/smartq)
[![docs](https://pushrocks.gitlab.io/assets/repo-button-docs.svg)](https://pushrocks.gitlab.io/smartq/)
2016-12-16 20:19:01 +00:00
## Status for master
2018-03-16 09:24:12 +00:00
2016-12-16 20:19:01 +00:00
[![build status](https://GitLab.com/pushrocks/smartq/badges/master/build.svg)](https://GitLab.com/pushrocks/smartq/commits/master)
[![coverage report](https://GitLab.com/pushrocks/smartq/badges/master/coverage.svg)](https://GitLab.com/pushrocks/smartq/commits/master)
[![npm downloads per month](https://img.shields.io/npm/dm/smartq.svg)](https://www.npmjs.com/package/smartq)
[![Dependency Status](https://david-dm.org/pushrocks/smartq.svg)](https://david-dm.org/pushrocks/smartq)
[![bitHound Dependencies](https://www.bithound.io/github/pushrocks/smartq/badges/dependencies.svg)](https://www.bithound.io/github/pushrocks/smartq/master/dependencies/npm)
[![bitHound Code](https://www.bithound.io/github/pushrocks/smartq/badges/code.svg)](https://www.bithound.io/github/pushrocks/smartq)
[![TypeScript](https://img.shields.io/badge/TypeScript-2.x-blue.svg)](https://nodejs.org/dist/latest-v6.x/docs/api/)
2017-07-06 12:55:42 +00:00
[![node](https://img.shields.io/badge/node->=%208.x.x-blue.svg)](https://nodejs.org/dist/latest-v6.x/docs/api/)
2016-12-16 20:19:01 +00:00
[![JavaScript Style Guide](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/)
## Usage
2018-03-16 09:24:12 +00:00
2016-12-16 20:19:01 +00:00
Use TypeScript for best in class instellisense.
> Note: smartq uses native ES6 promises
2017-01-20 23:45:11 +00:00
> smartq does not repeat any native functions, so for things like .all() simply use Promise.all()
```javascript
import * as q from 'smartq'
2017-01-28 15:37:54 +00:00
// Deferred
// -----------------------------------------------
let myAsyncFunction = (): Promise<string> => {
2017-01-20 21:01:10 +00:00
let done = q.defer<string>() // returns your typical Deferred object
setTimeout(() => {
2017-01-20 21:01:10 +00:00
done.resolve('hi') // will throw type error for other types than string as argument ;)
},6000)
return done.promise
}
let myAsyncFunction2 = async () => {
let aString = await myAsyncFunction()
console.log(aString) // will log 'hi' to console
}
myAsyncFunction2();
2017-01-28 15:37:54 +00:00
// Resolved and Rejected promises
// ------------------------------------------------
q.resolvedPromise(`I'll get logged to console soon`)
.then(x => {
console.log(x)
})
q.rejectedPromise(`what a lovely error message`)
.then(() => {
console.log('This never makes it to console')
}/*, alternatively put a reject function here */)
.catch(err => {
console.log(err)
})
2017-01-28 15:37:54 +00:00
// Promisify (typed)
// ------------------------------------------------
let myCallbackedFunction = (someString: string, someNumber: number, cb) => {
cb(null, someString)
}
let myPromisedFunction = q.promisify(myCallbackFunction)
myPromisedFunction('helloThere', 2).then(x => {
console.log(x) // will log 'helloThere' to console
})
```
2017-02-12 11:56:22 +00:00
For further information read the linked docs at the top of this README.
> MIT licensed | **&copy;** [Lossless GmbH](https://lossless.gmbh)
[![repo-footer](https://pushrocks.gitlab.io/assets/repo-footer.svg)](https://push.rocks)