2016-12-16 20:19:01 +00:00
|
|
|
# smartq
|
|
|
|
dropin replacement for q
|
|
|
|
|
|
|
|
## Availabililty
|
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
|
|
|
|
[![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/)
|
|
|
|
[![node](https://img.shields.io/badge/node->=%206.x.x-blue.svg)](https://nodejs.org/dist/latest-v6.x/docs/api/)
|
|
|
|
[![JavaScript Style Guide](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/)
|
|
|
|
|
|
|
|
## Usage
|
|
|
|
Use TypeScript for best in class instellisense.
|
|
|
|
|
2017-01-20 20:58:53 +00:00
|
|
|
> 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()
|
2017-01-20 20:58:53 +00:00
|
|
|
|
|
|
|
```javascript
|
|
|
|
import * as q from 'smartq'
|
|
|
|
|
2017-01-28 15:37:54 +00:00
|
|
|
// Deferred
|
|
|
|
// -----------------------------------------------
|
2017-01-20 20:58:53 +00:00
|
|
|
let myAsyncFunction = (): Promise<string> => {
|
2017-01-20 21:01:10 +00:00
|
|
|
let done = q.defer<string>() // returns your typical Deferred object
|
2017-01-20 20:58:53 +00:00
|
|
|
setTimeout(() => {
|
2017-01-20 21:01:10 +00:00
|
|
|
done.resolve('hi') // will throw type error for other types than string as argument ;)
|
2017-01-20 20:58:53 +00:00
|
|
|
},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
|
|
|
|
// ------------------------------------------------
|
2017-01-20 20:58:53 +00:00
|
|
|
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-01-20 20:58:53 +00:00
|
|
|
```
|
|
|
|
|
2017-01-28 15:37:54 +00:00
|
|
|
## Dependency Credits:
|
|
|
|
who | made what
|
|
|
|
-- | --
|
|
|
|
[notenoughneon](https://www.npmjs.com/~notenoughneon) | [typed-promisify](https://www.npmjs.com/package/typed-promisify)
|
|
|
|
|
2017-02-12 11:56:22 +00:00
|
|
|
For further information read the linked docs at the top of this README.
|
|
|
|
|
|
|
|
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
|
|
|
|
|
|
|
|
[![repo-footer](https://pushrocks.gitlab.io/assets/repo-footer.svg)](https://push.rocks)
|