Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
1363aa7957 | |||
095ea36e5d | |||
b6ab079de0 | |||
734d5aca35 | |||
3b0052602f | |||
9693ec04d6 | |||
4ee932873c | |||
3bedd3581e |
2
package-lock.json
generated
2
package-lock.json
generated
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@pushrocks/smartpromise",
|
||||
"version": "3.0.9",
|
||||
"version": "3.1.3",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
|
@ -1,13 +1,13 @@
|
||||
{
|
||||
"name": "@pushrocks/smartpromise",
|
||||
"private": false,
|
||||
"version": "3.0.9",
|
||||
"version": "3.1.3",
|
||||
"description": "simple promises and Deferred constructs",
|
||||
"main": "dist_ts/index.js",
|
||||
"typings": "dist_ts/index.d.ts",
|
||||
"scripts": {
|
||||
"test": "(tstest test/)",
|
||||
"build": "(tsbuild)"
|
||||
"test": "(tstest test/ --web)",
|
||||
"build": "(tsbuild --web)"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
|
90
readme.md
Normal file
90
readme.md
Normal file
@ -0,0 +1,90 @@
|
||||
# @pushrocks/smartpromise
|
||||
|
||||
simple promises and Deferred constructs
|
||||
|
||||
## Availabililty and Links
|
||||
|
||||
- [npmjs.org (npm package)](https://www.npmjs.com/package/@pushrocks/smartpromise)
|
||||
- [gitlab.com (source)](https://gitlab.com/pushrocks/smartpromise)
|
||||
- [github.com (source mirror)](https://github.com/pushrocks/smartpromise)
|
||||
- [docs (typedoc)](https://pushrocks.gitlab.io/smartpromise/)
|
||||
|
||||
## Status for master
|
||||
|
||||
[](https://gitlab.com/pushrocks/smartpromise/commits/master)
|
||||
[](https://gitlab.com/pushrocks/smartpromise/commits/master)
|
||||
[](https://www.npmjs.com/package/@pushrocks/smartpromise)
|
||||
[](https://snyk.io/test/npm/@pushrocks/smartpromise)
|
||||
[](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
||||
[](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
||||
[](https://prettier.io/)
|
||||
|
||||
## Usage
|
||||
|
||||
Use TypeScript for best in class instellisense.
|
||||
|
||||
> Note: smartq uses native ES6 promises
|
||||
> smartq does not repeat any native functions, so for things like .all() simply use Promise.all()
|
||||
|
||||
```typescript
|
||||
import * as q from '@pushrocks/smartpromise'
|
||||
|
||||
// Deferred
|
||||
// -----------------------------------------------
|
||||
let myAsyncFunction = (): Promise<string> => {
|
||||
let done = q.defer<string>() // returns your typical Deferred object
|
||||
setTimeout(() => {
|
||||
done.resolve('hi') // will throw type error for other types than string as argument ;)
|
||||
},6000);
|
||||
|
||||
console.log(done.status) // logs "pending";
|
||||
done.promise.then(() => {
|
||||
console.log(done.status) // logs "fullfilled"
|
||||
console.log(done.duration) // logs the milliseconds between instantiation and fullfillment
|
||||
})
|
||||
|
||||
return done.promise
|
||||
}
|
||||
|
||||
let myAsyncFunction2 = async () => {
|
||||
let aString = await myAsyncFunction()
|
||||
console.log(aString) // will log 'hi' to console
|
||||
}
|
||||
|
||||
myAsyncFunction2();
|
||||
|
||||
|
||||
// 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)
|
||||
})
|
||||
|
||||
// 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
|
||||
})
|
||||
```
|
||||
|
||||
For further information read the linked docs at the top of this readme.
|
||||
|
||||
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
|
||||
> | By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy)
|
||||
|
||||
[](https://maintainedby.lossless.com)
|
@ -84,11 +84,11 @@ export const map = async <T>(inputArg: T[], functionArg: IAsyncFunction<T>) => {
|
||||
return resultArray;
|
||||
};
|
||||
|
||||
export const timeoutWrap = (ms, promise) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
export const timeoutWrap = <T = any>(promiseArg: Promise<T>, timeoutInMs: number) => {
|
||||
return new Promise<T>((resolve, reject) => {
|
||||
setTimeout(() => {
|
||||
reject(new Error('timeout'));
|
||||
}, ms);
|
||||
promise.then(resolve, reject);
|
||||
}, timeoutInMs);
|
||||
promiseArg.then(resolve, reject);
|
||||
});
|
||||
};
|
||||
|
Reference in New Issue
Block a user