Compare commits
42 Commits
Author | SHA1 | Date | |
---|---|---|---|
3340f1a895 | |||
ee336af699 | |||
0526c45ea1 | |||
9933d72784 | |||
d1bc689b35 | |||
25c0deb3f3 | |||
5138219563 | |||
93f426cce0 | |||
e42061047a | |||
3b07f99488 | |||
3d252a5e3e | |||
d82722296b | |||
22317ef83d | |||
054ec0afa4 | |||
19d434533f | |||
9b8440f743 | |||
564953e25f | |||
98f190ea21 | |||
2b77c8e019 | |||
d8e5fbf3bc | |||
1069d5a38e | |||
eb82d16859 | |||
082f533975 | |||
54b2fb0ae5 | |||
31c7e607cc | |||
b15cdb48a3 | |||
7c1605eccf | |||
cea619e964 | |||
fb866b36af | |||
a8934950ef | |||
590dc27d20 | |||
3bcff82d31 | |||
bfbea0acde | |||
ba18b31f2f | |||
4fc619970f | |||
e0cfa6ca29 | |||
843d217698 | |||
dbd2fe73b8 | |||
1eea124cf4 | |||
6a4ee22f36 | |||
e23f946e50 | |||
8d62fc6ef1 |
25
.gitignore
vendored
25
.gitignore
vendored
@ -1,5 +1,22 @@
|
||||
coverage/
|
||||
pages/
|
||||
node_modules/
|
||||
public/
|
||||
.nogit/
|
||||
|
||||
# artifacts
|
||||
coverage/
|
||||
public/
|
||||
pages/
|
||||
|
||||
# installs
|
||||
node_modules/
|
||||
|
||||
# caches
|
||||
.yarn/
|
||||
.cache/
|
||||
.rpt2_cache
|
||||
|
||||
# builds
|
||||
dist/
|
||||
dist_web/
|
||||
dist_serve/
|
||||
dist_ts_web/
|
||||
|
||||
# custom
|
110
.gitlab-ci.yml
110
.gitlab-ci.yml
@ -1,59 +1,119 @@
|
||||
image: hosttoday/ht-docker-node:npmts
|
||||
# gitzone ci_default
|
||||
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||
|
||||
cache:
|
||||
paths:
|
||||
- .npmci_cache/
|
||||
key: "$CI_BUILD_STAGE"
|
||||
|
||||
stages:
|
||||
- security
|
||||
- test
|
||||
- release
|
||||
- trigger
|
||||
- pages
|
||||
- metadata
|
||||
|
||||
testLEGACY:
|
||||
stage: test
|
||||
# ====================
|
||||
# security stage
|
||||
# ====================
|
||||
mirror:
|
||||
stage: security
|
||||
script:
|
||||
- npmci test legacy
|
||||
- npmci git mirror
|
||||
tags:
|
||||
- docker
|
||||
allow_failure: true
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
testLTS:
|
||||
snyk:
|
||||
stage: security
|
||||
script:
|
||||
- npmci npm prepare
|
||||
- npmci command npm install -g snyk
|
||||
- npmci command npm install --ignore-scripts
|
||||
- npmci command snyk test
|
||||
tags:
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
# ====================
|
||||
# test stage
|
||||
# ====================
|
||||
|
||||
testStable:
|
||||
stage: test
|
||||
script:
|
||||
- npmci test lts
|
||||
- npmci npm prepare
|
||||
- npmci node install stable
|
||||
- npmci npm install
|
||||
- npmci npm test
|
||||
coverage: /\d+.?\d+?\%\s*coverage/
|
||||
tags:
|
||||
- docker
|
||||
|
||||
testSTABLE:
|
||||
- docker
|
||||
- priv
|
||||
|
||||
testBuild:
|
||||
stage: test
|
||||
script:
|
||||
- npmci test stable
|
||||
- npmci npm prepare
|
||||
- npmci node install stable
|
||||
- npmci npm install
|
||||
- npmci command npm run build
|
||||
coverage: /\d+.?\d+?\%\s*coverage/
|
||||
tags:
|
||||
- docker
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
release:
|
||||
stage: release
|
||||
script:
|
||||
- npmci publish
|
||||
- npmci node install stable
|
||||
- npmci npm publish
|
||||
only:
|
||||
- tags
|
||||
- tags
|
||||
tags:
|
||||
- docker
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
# ====================
|
||||
# metadata stage
|
||||
# ====================
|
||||
codequality:
|
||||
stage: metadata
|
||||
allow_failure: true
|
||||
script:
|
||||
- npmci command npm install -g tslint typescript
|
||||
- npmci npm install
|
||||
- npmci command "tslint -c tslint.json ./ts/**/*.ts"
|
||||
tags:
|
||||
- docker
|
||||
- priv
|
||||
|
||||
trigger:
|
||||
stage: trigger
|
||||
stage: metadata
|
||||
script:
|
||||
- npmci trigger
|
||||
- npmci trigger
|
||||
only:
|
||||
- tags
|
||||
- tags
|
||||
tags:
|
||||
- docker
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
pages:
|
||||
image: hosttoday/ht-docker-node:npmpage
|
||||
stage: pages
|
||||
image: hosttoday/ht-docker-dbase:npmci
|
||||
services:
|
||||
- docker:stable-dind
|
||||
stage: metadata
|
||||
script:
|
||||
- npmci command npmpage --host gitlab
|
||||
- npmci command npm install -g @gitzone/tsdoc
|
||||
- npmci npm prepare
|
||||
- npmci npm install
|
||||
- npmci command tsdoc
|
||||
tags:
|
||||
- docker
|
||||
- notpriv
|
||||
only:
|
||||
- tags
|
||||
artifacts:
|
||||
expire_in: 1 week
|
||||
paths:
|
||||
- public
|
||||
allow_failure: true
|
||||
|
86
README.md
86
README.md
@ -1,37 +1,49 @@
|
||||
# smartq
|
||||
dropin replacement for q
|
||||
# @pushrocks/smartpromise
|
||||
|
||||
## Availabililty
|
||||
[](https://www.npmjs.com/package/smartq)
|
||||
[](https://GitLab.com/pushrocks/smartq)
|
||||
[](https://github.com/pushrocks/smartq)
|
||||
[](https://pushrocks.gitlab.io/smartq/)
|
||||
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/smartq/commits/master)
|
||||
[](https://GitLab.com/pushrocks/smartq/commits/master)
|
||||
[](https://www.npmjs.com/package/smartq)
|
||||
[](https://david-dm.org/pushrocks/smartq)
|
||||
[](https://www.bithound.io/github/pushrocks/smartq/master/dependencies/npm)
|
||||
[](https://www.bithound.io/github/pushrocks/smartq)
|
||||
[](https://nodejs.org/dist/latest-v6.x/docs/api/)
|
||||
[](https://nodejs.org/dist/latest-v6.x/docs/api/)
|
||||
[](http://standardjs.com/)
|
||||
|
||||
[](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()
|
||||
|
||||
```javascript
|
||||
import * as q from 'smartq'
|
||||
```typescript
|
||||
import * as q from '@pushrocks/smartpromise'
|
||||
|
||||
// Deferred
|
||||
// -----------------------------------------------
|
||||
let myAsyncFunction = (): Promise<string> => {
|
||||
let done = q.defer() // returns your typical Deferred object
|
||||
setTimeout(() => {
|
||||
done.resolve('hi')
|
||||
},6000)
|
||||
return done.promise
|
||||
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 () => {
|
||||
@ -40,16 +52,10 @@ let myAsyncFunction2 = async () => {
|
||||
}
|
||||
|
||||
myAsyncFunction2();
|
||||
q.all(myAsyncFunction(), myAsyncFunction2())
|
||||
.then(() => {
|
||||
console.log('all promises for q.all have been fullfilled')
|
||||
})
|
||||
|
||||
q.race(/* some promises here */)
|
||||
.then(() => {
|
||||
console.log('at least one promise for q.race is fullfilled')
|
||||
})
|
||||
|
||||
// Resolved and Rejected promises
|
||||
// ------------------------------------------------
|
||||
q.resolvedPromise(`I'll get logged to console soon`)
|
||||
.then(x => {
|
||||
console.log(x)
|
||||
@ -63,6 +69,22 @@ q.rejectedPromise(`what a lovely error message`)
|
||||
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
|
||||
})
|
||||
```
|
||||
|
||||
[](https://push.rocks)
|
||||
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)
|
||||
|
60
dist/index.d.ts
vendored
60
dist/index.d.ts
vendored
@ -1,60 +0,0 @@
|
||||
import 'typings-global';
|
||||
export interface IResolve<T> {
|
||||
(value?: T | Promise<T>): void;
|
||||
}
|
||||
export interface IReject {
|
||||
(reason?: any): void;
|
||||
}
|
||||
export declare class Deferred<T> {
|
||||
promise: Promise<T>;
|
||||
resolve: IResolve<T>;
|
||||
reject: IReject;
|
||||
constructor();
|
||||
}
|
||||
export declare let defer: <T>() => Deferred<T>;
|
||||
/**
|
||||
* Creates a Promise that is resolved with an array of results when all of the provided Promises resolve, or rejected when any Promise is rejected.
|
||||
*/
|
||||
export declare let all: {
|
||||
<TAll>(values: Iterable<TAll | PromiseLike<TAll>>): Promise<TAll[]>;
|
||||
<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike<T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>, T7 | PromiseLike<T7>, T8 | PromiseLike<T8>, T9 | PromiseLike<T9>, T10 | PromiseLike<T10>]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10]>;
|
||||
<T1, T2, T3, T4, T5, T6, T7, T8, T9>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike<T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>, T7 | PromiseLike<T7>, T8 | PromiseLike<T8>, T9 | PromiseLike<T9>]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8, T9]>;
|
||||
<T1, T2, T3, T4, T5, T6, T7, T8>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike<T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>, T7 | PromiseLike<T7>, T8 | PromiseLike<T8>]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8]>;
|
||||
<T1, T2, T3, T4, T5, T6, T7>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike<T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>, T7 | PromiseLike<T7>]): Promise<[T1, T2, T3, T4, T5, T6, T7]>;
|
||||
<T1, T2, T3, T4, T5, T6>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike<T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>]): Promise<[T1, T2, T3, T4, T5, T6]>;
|
||||
<T1, T2, T3, T4, T5>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike<T4>, T5 | PromiseLike<T5>]): Promise<[T1, T2, T3, T4, T5]>;
|
||||
<T1, T2, T3, T4>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike<T4>]): Promise<[T1, T2, T3, T4]>;
|
||||
<T1, T2, T3>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>]): Promise<[T1, T2, T3]>;
|
||||
<T1, T2>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>]): Promise<[T1, T2]>;
|
||||
<T>(values: (T | PromiseLike<T>)[]): Promise<T[]>;
|
||||
};
|
||||
/**
|
||||
* Creates a Promise that is resolved or rejected when any of the provided Promises are resolved or rejected.
|
||||
*/
|
||||
export declare let race: {
|
||||
<T>(values: Iterable<T | PromiseLike<T>>): Promise<T>;
|
||||
<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike<T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>, T7 | PromiseLike<T7>, T8 | PromiseLike<T8>, T9 | PromiseLike<T9>, T10 | PromiseLike<T10>]): Promise<T1 | T2 | T3 | T4 | T5 | T6 | T7 | T8 | T9 | T10>;
|
||||
<T1, T2, T3, T4, T5, T6, T7, T8, T9>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike<T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>, T7 | PromiseLike<T7>, T8 | PromiseLike<T8>, T9 | PromiseLike<T9>]): Promise<T1 | T2 | T3 | T4 | T5 | T6 | T7 | T8 | T9>;
|
||||
<T1, T2, T3, T4, T5, T6, T7, T8>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike<T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>, T7 | PromiseLike<T7>, T8 | PromiseLike<T8>]): Promise<T1 | T2 | T3 | T4 | T5 | T6 | T7 | T8>;
|
||||
<T1, T2, T3, T4, T5, T6, T7>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike<T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>, T7 | PromiseLike<T7>]): Promise<T1 | T2 | T3 | T4 | T5 | T6 | T7>;
|
||||
<T1, T2, T3, T4, T5, T6>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike<T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>]): Promise<T1 | T2 | T3 | T4 | T5 | T6>;
|
||||
<T1, T2, T3, T4, T5>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike<T4>, T5 | PromiseLike<T5>]): Promise<T1 | T2 | T3 | T4 | T5>;
|
||||
<T1, T2, T3, T4>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike<T4>]): Promise<T1 | T2 | T3 | T4>;
|
||||
<T1, T2, T3>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>]): Promise<T1 | T2 | T3>;
|
||||
<T1, T2>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>]): Promise<T1 | T2>;
|
||||
<T>(values: (T | PromiseLike<T>)[]): Promise<T>;
|
||||
};
|
||||
/**
|
||||
* Creates a new resolved promise for the provided value.
|
||||
*/
|
||||
export declare let resolvedPromise: {
|
||||
<T>(value: T | PromiseLike<T>): Promise<T>;
|
||||
(): Promise<void>;
|
||||
};
|
||||
/**
|
||||
* Creates a new rejected promise for the provided reason.
|
||||
*/
|
||||
export declare let rejectedPromise: {
|
||||
(reason: any): Promise<never>;
|
||||
<T>(reason: any): Promise<T>;
|
||||
};
|
31
dist/index.js
vendored
31
dist/index.js
vendored
@ -1,31 +0,0 @@
|
||||
"use strict";
|
||||
require("typings-global");
|
||||
class Deferred {
|
||||
constructor() {
|
||||
this.promise = new Promise((resolve, reject) => {
|
||||
this.resolve = resolve;
|
||||
this.reject = reject;
|
||||
});
|
||||
}
|
||||
}
|
||||
exports.Deferred = Deferred;
|
||||
exports.defer = () => {
|
||||
return new Deferred();
|
||||
};
|
||||
/**
|
||||
* Creates a Promise that is resolved with an array of results when all of the provided Promises resolve, or rejected when any Promise is rejected.
|
||||
*/
|
||||
exports.all = Promise.all;
|
||||
/**
|
||||
* Creates a Promise that is resolved or rejected when any of the provided Promises are resolved or rejected.
|
||||
*/
|
||||
exports.race = Promise.race;
|
||||
/**
|
||||
* Creates a new resolved promise for the provided value.
|
||||
*/
|
||||
exports.resolvedPromise = Promise.resolve;
|
||||
/**
|
||||
* Creates a new rejected promise for the provided reason.
|
||||
*/
|
||||
exports.rejectedPromise = Promise.reject;
|
||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUEsMEJBQXVCO0FBVXZCO0lBSUk7UUFDSSxJQUFJLENBQUMsT0FBTyxHQUFHLElBQUksT0FBTyxDQUFJLENBQUMsT0FBTyxFQUFFLE1BQU07WUFDMUMsSUFBSSxDQUFDLE9BQU8sR0FBRyxPQUFPLENBQUE7WUFDdEIsSUFBSSxDQUFDLE1BQU0sR0FBRyxNQUFNLENBQUE7UUFDeEIsQ0FBQyxDQUFDLENBQUE7SUFDTixDQUFDO0NBQ0o7QUFWRCw0QkFVQztBQUVVLFFBQUEsS0FBSyxHQUFHO0lBQ2YsTUFBTSxDQUFDLElBQUksUUFBUSxFQUFLLENBQUE7QUFDNUIsQ0FBQyxDQUFBO0FBRUQ7O0dBRUc7QUFDUSxRQUFBLEdBQUcsR0FBRyxPQUFPLENBQUMsR0FBRyxDQUFBO0FBRTVCOztHQUVHO0FBQ1EsUUFBQSxJQUFJLEdBQUcsT0FBTyxDQUFDLElBQUksQ0FBQTtBQUU5Qjs7R0FFRztBQUNRLFFBQUEsZUFBZSxHQUFHLE9BQU8sQ0FBQyxPQUFPLENBQUE7QUFFNUM7O0dBRUc7QUFDUSxRQUFBLGVBQWUsR0FBRyxPQUFPLENBQUMsTUFBTSxDQUFBIn0=
|
16
npmextra.json
Normal file
16
npmextra.json
Normal file
@ -0,0 +1,16 @@
|
||||
{
|
||||
"npmci": {
|
||||
"npmGlobalTools": [],
|
||||
"npmAccessLevel": "public"
|
||||
},
|
||||
"gitzone": {
|
||||
"module": {
|
||||
"githost": "gitlab.com",
|
||||
"gitscope": "pushrocks",
|
||||
"gitrepo": "smartpromise",
|
||||
"shortDescription": "simple promises and Deferred constructs",
|
||||
"npmPackagename": "@pushrocks/smartpromise",
|
||||
"license": "MIT"
|
||||
}
|
||||
}
|
||||
}
|
1718
package-lock.json
generated
Normal file
1718
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
36
package.json
36
package.json
@ -1,11 +1,13 @@
|
||||
{
|
||||
"name": "smartq",
|
||||
"version": "1.0.3",
|
||||
"description": "dropin replacement for q",
|
||||
"name": "@pushrocks/smartpromise",
|
||||
"private": false,
|
||||
"version": "3.0.3",
|
||||
"description": "simple promises and Deferred constructs",
|
||||
"main": "dist/index.js",
|
||||
"typings": "dist/index.d.ts",
|
||||
"scripts": {
|
||||
"test": "(npmts)"
|
||||
"test": "(tstest test/)",
|
||||
"build": "(tsbuild)"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
@ -17,12 +19,24 @@
|
||||
"url": "https://gitlab.com/pushrocks/smartq/issues"
|
||||
},
|
||||
"homepage": "https://gitlab.com/pushrocks/smartq#README",
|
||||
"dependencies": {
|
||||
"typings-global": "^1.0.14"
|
||||
},
|
||||
"dependencies": {},
|
||||
"devDependencies": {
|
||||
"@types/should": "^8.1.30",
|
||||
"should": "^11.1.2",
|
||||
"typings-test": "^1.0.3"
|
||||
}
|
||||
"@gitzone/tsbuild": "^2.1.17",
|
||||
"@gitzone/tstest": "^1.0.24",
|
||||
"@pushrocks/tapbundle": "^3.0.13",
|
||||
"@types/node": "^10.5.1",
|
||||
"tslint": "^5.20.0",
|
||||
"tslint-config-prettier": "^1.18.0"
|
||||
},
|
||||
"files": [
|
||||
"ts/*",
|
||||
"ts_web/*",
|
||||
"dist/*",
|
||||
"dist_web/*",
|
||||
"dist_ts_web/*",
|
||||
"assets/*",
|
||||
"cli.js",
|
||||
"npmextra.json",
|
||||
"readme.md"
|
||||
]
|
||||
}
|
||||
|
1
test/test.d.ts
vendored
1
test/test.d.ts
vendored
@ -1 +0,0 @@
|
||||
import 'typings-test';
|
23
test/test.js
23
test/test.js
@ -1,23 +0,0 @@
|
||||
"use strict";
|
||||
require("typings-test");
|
||||
const should = require("should");
|
||||
const q = require("../dist/index");
|
||||
describe('smartq', function () {
|
||||
it('should return a Deferred for .defer()', function (done) {
|
||||
let myDeferred = q.defer();
|
||||
myDeferred.promise.then(() => {
|
||||
done();
|
||||
});
|
||||
myDeferred.resolve();
|
||||
});
|
||||
it('should let types flow through the Promise', function (done) {
|
||||
let myString = 'someString';
|
||||
let myDeferred = q.defer();
|
||||
myDeferred.promise.then(x => {
|
||||
should(x).equal('someString');
|
||||
done();
|
||||
});
|
||||
myDeferred.resolve(myString);
|
||||
});
|
||||
});
|
||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGVzdC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbInRlc3QudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLHdCQUFxQjtBQUVyQixpQ0FBZ0M7QUFDaEMsbUNBQWtDO0FBRWxDLFFBQVEsQ0FBQyxRQUFRLEVBQUU7SUFDZixFQUFFLENBQUMsdUNBQXVDLEVBQUUsVUFBUyxJQUFJO1FBQ3JELElBQUksVUFBVSxHQUFHLENBQUMsQ0FBQyxLQUFLLEVBQUUsQ0FBQTtRQUMxQixVQUFVLENBQUMsT0FBTyxDQUFDLElBQUksQ0FBQztZQUNwQixJQUFJLEVBQUUsQ0FBQTtRQUNWLENBQUMsQ0FBQyxDQUFBO1FBQ0YsVUFBVSxDQUFDLE9BQU8sRUFBRSxDQUFBO0lBQ3hCLENBQUMsQ0FBQyxDQUFBO0lBRUYsRUFBRSxDQUFDLDJDQUEyQyxFQUFFLFVBQVMsSUFBSTtRQUN6RCxJQUFJLFFBQVEsR0FBRyxZQUFZLENBQUE7UUFDM0IsSUFBSSxVQUFVLEdBQUcsQ0FBQyxDQUFDLEtBQUssRUFBVSxDQUFBO1FBQ2xDLFVBQVUsQ0FBQyxPQUFPLENBQUMsSUFBSSxDQUFDLENBQUM7WUFDckIsTUFBTSxDQUFDLENBQUMsQ0FBQyxDQUFDLEtBQUssQ0FBQyxZQUFZLENBQUMsQ0FBQTtZQUM3QixJQUFJLEVBQUUsQ0FBQTtRQUNWLENBQUMsQ0FBQyxDQUFBO1FBQ0YsVUFBVSxDQUFDLE9BQU8sQ0FBQyxRQUFRLENBQUMsQ0FBQTtJQUNoQyxDQUFDLENBQUMsQ0FBQTtBQUNOLENBQUMsQ0FBQyxDQUFBIn0=
|
48
test/test.ts
48
test/test.ts
@ -1,24 +1,30 @@
|
||||
import 'typings-test'
|
||||
import { expect, tap } from '@pushrocks/tapbundle';
|
||||
import * as q from '../ts/index';
|
||||
|
||||
import * as should from 'should'
|
||||
import * as q from '../dist/index'
|
||||
tap.test('should return a Deferred for .defer()', async () => {
|
||||
let myDeferred = q.defer();
|
||||
let expectPromise = expect(myDeferred.promise).to.eventually.be.fulfilled;
|
||||
myDeferred.resolve();
|
||||
return expectPromise;
|
||||
});
|
||||
|
||||
describe('smartq', function() {
|
||||
it('should return a Deferred for .defer()', function(done) {
|
||||
let myDeferred = q.defer()
|
||||
myDeferred.promise.then(() => {
|
||||
done()
|
||||
})
|
||||
myDeferred.resolve()
|
||||
})
|
||||
tap.test('should let types flow through the Promise', async () => {
|
||||
let myString = 'someString';
|
||||
let myDeferred = q.defer<string>();
|
||||
let expectPromise = expect(myDeferred.promise).to.eventually.equal('someString');
|
||||
myDeferred.resolve(myString);
|
||||
return expectPromise;
|
||||
});
|
||||
|
||||
it('should let types flow through the Promise', function(done) {
|
||||
let myString = 'someString'
|
||||
let myDeferred = q.defer<string>()
|
||||
myDeferred.promise.then(x => {
|
||||
should(x).equal('someString')
|
||||
done()
|
||||
})
|
||||
myDeferred.resolve(myString)
|
||||
})
|
||||
})
|
||||
tap.test('should map callbacks', async () => {
|
||||
let inputArray = ['hi', 'awesome'];
|
||||
const myPromisified = async myInput => {
|
||||
return myInput;
|
||||
};
|
||||
const expectPromise = expect(q.map(inputArray, myPromisified)).to.eventually.deep.equal(
|
||||
inputArray
|
||||
);
|
||||
return expectPromise;
|
||||
});
|
||||
|
||||
tap.start();
|
||||
|
86
ts/index.ts
86
ts/index.ts
@ -1,45 +1,79 @@
|
||||
import 'typings-global'
|
||||
|
||||
export interface IResolve<T> {
|
||||
(value?: T | Promise<T>): void
|
||||
(value?: T | Promise<T>): void;
|
||||
}
|
||||
|
||||
export interface IReject {
|
||||
(reason?: any): void
|
||||
(reason?: any): void;
|
||||
}
|
||||
|
||||
export type TDeferredStatus = 'pending' | 'fulfilled' | 'rejected';
|
||||
|
||||
export class Deferred<T> {
|
||||
promise: Promise<T>
|
||||
resolve: IResolve<T>
|
||||
reject: IReject
|
||||
constructor() {
|
||||
this.promise = new Promise<T>((resolve, reject) => {
|
||||
this.resolve = resolve
|
||||
this.reject = reject
|
||||
})
|
||||
public promise: Promise<T>;
|
||||
public resolve: IResolve<T>;
|
||||
public reject: IReject;
|
||||
public status: TDeferredStatus;
|
||||
|
||||
public startedAt: number;
|
||||
public stoppedAt: number;
|
||||
public get duration(): number {
|
||||
if (this.stoppedAt) {
|
||||
return this.stoppedAt - this.startedAt;
|
||||
} else {
|
||||
return Date.now() - this.startedAt;
|
||||
}
|
||||
}
|
||||
|
||||
constructor() {
|
||||
this.promise = new Promise<T>((resolve, reject) => {
|
||||
this.resolve = (valueArg: T | PromiseLike<T>) => {
|
||||
this.status = 'fulfilled';
|
||||
this.stoppedAt = Date.now();
|
||||
resolve(valueArg);
|
||||
};
|
||||
this.reject = (reason: any) => {
|
||||
this.status = 'rejected';
|
||||
this.stoppedAt = Date.now();
|
||||
reject(reason);
|
||||
};
|
||||
this.startedAt = Date.now();
|
||||
this.status = 'pending';
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export let defer = <T>() => {
|
||||
return new Deferred<T>()
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a Promise that is resolved with an array of results when all of the provided Promises resolve, or rejected when any Promise is rejected.
|
||||
*/
|
||||
export let all = Promise.all
|
||||
|
||||
/**
|
||||
* Creates a Promise that is resolved or rejected when any of the provided Promises are resolved or rejected.
|
||||
*/
|
||||
export let race = Promise.race
|
||||
return new Deferred<T>();
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a new resolved promise for the provided value.
|
||||
*/
|
||||
export let resolvedPromise = Promise.resolve
|
||||
export let resolvedPromise = <T>(value?: T): Promise<T> => {
|
||||
return Promise.resolve(value);
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a new rejected promise for the provided reason.
|
||||
*/
|
||||
export let rejectedPromise = Promise.reject
|
||||
export let rejectedPromise = err => {
|
||||
return Promise.reject(err);
|
||||
};
|
||||
|
||||
interface IAsyncFunction<T> {
|
||||
(someArg: T): Promise<T>;
|
||||
}
|
||||
|
||||
export let map = async <T>(inputArg: T[], functionArg: IAsyncFunction<T>) => {
|
||||
let promiseArray: Promise<any>[] = [];
|
||||
let resultArray = [];
|
||||
for (let item of inputArg) {
|
||||
let promise: Promise<any> = functionArg(item);
|
||||
promiseArray.push(promise);
|
||||
promise.then(x => {
|
||||
resultArray.push(x);
|
||||
});
|
||||
}
|
||||
await Promise.all(promiseArray);
|
||||
return resultArray;
|
||||
};
|
||||
|
16
tslint.json
16
tslint.json
@ -1,3 +1,17 @@
|
||||
{
|
||||
"extends": "tslint-config-standard"
|
||||
"extends": ["tslint:latest", "tslint-config-prettier"],
|
||||
"rules": {
|
||||
"semicolon": [true, "always"],
|
||||
"no-console": false,
|
||||
"ordered-imports": false,
|
||||
"object-literal-sort-keys": false,
|
||||
"member-ordering": {
|
||||
"options":{
|
||||
"order": [
|
||||
"static-method"
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
"defaultSeverity": "warning"
|
||||
}
|
||||
|
Reference in New Issue
Block a user