Compare commits
16 Commits
Author | SHA1 | Date | |
---|---|---|---|
ddce50ac0f | |||
baf956d0ed | |||
3340f1a895 | |||
ee336af699 | |||
0526c45ea1 | |||
9933d72784 | |||
d1bc689b35 | |||
25c0deb3f3 | |||
5138219563 | |||
93f426cce0 | |||
e42061047a | |||
3b07f99488 | |||
3d252a5e3e | |||
d82722296b | |||
22317ef83d | |||
054ec0afa4 |
25
.gitignore
vendored
25
.gitignore
vendored
@ -1,5 +1,22 @@
|
|||||||
coverage/
|
.nogit/
|
||||||
pages/
|
|
||||||
node_modules/
|
|
||||||
public/
|
|
||||||
|
|
||||||
|
# artifacts
|
||||||
|
coverage/
|
||||||
|
public/
|
||||||
|
pages/
|
||||||
|
|
||||||
|
# installs
|
||||||
|
node_modules/
|
||||||
|
|
||||||
|
# caches
|
||||||
|
.yarn/
|
||||||
|
.cache/
|
||||||
|
.rpt2_cache
|
||||||
|
|
||||||
|
# builds
|
||||||
|
dist/
|
||||||
|
dist_web/
|
||||||
|
dist_serve/
|
||||||
|
dist_ts_web/
|
||||||
|
|
||||||
|
# custom
|
@ -1,5 +1,5 @@
|
|||||||
# gitzone standard
|
# gitzone ci_default
|
||||||
image: hosttoday/ht-docker-node:npmci
|
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||||
|
|
||||||
cache:
|
cache:
|
||||||
paths:
|
paths:
|
||||||
@ -26,6 +26,7 @@ mirror:
|
|||||||
snyk:
|
snyk:
|
||||||
stage: security
|
stage: security
|
||||||
script:
|
script:
|
||||||
|
- npmci npm prepare
|
||||||
- npmci command npm install -g snyk
|
- npmci command npm install -g snyk
|
||||||
- npmci command npm install --ignore-scripts
|
- npmci command npm install --ignore-scripts
|
||||||
- npmci command snyk test
|
- npmci command snyk test
|
||||||
@ -36,38 +37,29 @@ snyk:
|
|||||||
# ====================
|
# ====================
|
||||||
# test stage
|
# test stage
|
||||||
# ====================
|
# ====================
|
||||||
testLEGACY:
|
|
||||||
stage: test
|
|
||||||
script:
|
|
||||||
- npmci node install legacy
|
|
||||||
- npmci npm install
|
|
||||||
- npmci npm test
|
|
||||||
coverage: /\d+.?\d+?\%\s*coverage/
|
|
||||||
tags:
|
|
||||||
- docker
|
|
||||||
- notpriv
|
|
||||||
allow_failure: true
|
|
||||||
|
|
||||||
testLTS:
|
testStable:
|
||||||
stage: test
|
|
||||||
script:
|
|
||||||
- npmci node install lts
|
|
||||||
- npmci npm install
|
|
||||||
- npmci npm test
|
|
||||||
coverage: /\d+.?\d+?\%\s*coverage/
|
|
||||||
tags:
|
|
||||||
- docker
|
|
||||||
- notpriv
|
|
||||||
|
|
||||||
testSTABLE:
|
|
||||||
stage: test
|
stage: test
|
||||||
script:
|
script:
|
||||||
|
- npmci npm prepare
|
||||||
- npmci node install stable
|
- npmci node install stable
|
||||||
- npmci npm install
|
- npmci npm install
|
||||||
- npmci npm test
|
- npmci npm test
|
||||||
coverage: /\d+.?\d+?\%\s*coverage/
|
coverage: /\d+.?\d+?\%\s*coverage/
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- docker
|
||||||
|
- priv
|
||||||
|
|
||||||
|
testBuild:
|
||||||
|
stage: test
|
||||||
|
script:
|
||||||
|
- npmci npm prepare
|
||||||
|
- npmci node install stable
|
||||||
|
- npmci npm install
|
||||||
|
- npmci command npm run build
|
||||||
|
coverage: /\d+.?\d+?\%\s*coverage/
|
||||||
|
tags:
|
||||||
|
- docker
|
||||||
- notpriv
|
- notpriv
|
||||||
|
|
||||||
release:
|
release:
|
||||||
@ -86,19 +78,11 @@ release:
|
|||||||
# ====================
|
# ====================
|
||||||
codequality:
|
codequality:
|
||||||
stage: metadata
|
stage: metadata
|
||||||
image: docker:stable
|
|
||||||
allow_failure: true
|
allow_failure: true
|
||||||
services:
|
|
||||||
- docker:stable-dind
|
|
||||||
script:
|
script:
|
||||||
- export SP_VERSION=$(echo "$CI_SERVER_VERSION" | sed 's/^\([0-9]*\)\.\([0-9]*\).*/\1-\2-stable/')
|
- npmci command npm install -g tslint typescript
|
||||||
- docker run
|
- npmci npm install
|
||||||
--env SOURCE_CODE="$PWD"
|
- npmci command "tslint -c tslint.json ./ts/**/*.ts"
|
||||||
--volume "$PWD":/code
|
|
||||||
--volume /var/run/docker.sock:/var/run/docker.sock
|
|
||||||
"registry.gitlab.com/gitlab-org/security-products/codequality:$SP_VERSION" /code
|
|
||||||
artifacts:
|
|
||||||
paths: [codeclimate.json]
|
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- docker
|
||||||
- priv
|
- priv
|
||||||
@ -114,11 +98,15 @@ trigger:
|
|||||||
- notpriv
|
- notpriv
|
||||||
|
|
||||||
pages:
|
pages:
|
||||||
image: hosttoday/ht-docker-node:npmci
|
image: hosttoday/ht-docker-dbase:npmci
|
||||||
|
services:
|
||||||
|
- docker:stable-dind
|
||||||
stage: metadata
|
stage: metadata
|
||||||
script:
|
script:
|
||||||
- npmci command npm install -g npmpage
|
- npmci command npm install -g @gitzone/tsdoc
|
||||||
- npmci command npmpage
|
- npmci npm prepare
|
||||||
|
- npmci npm install
|
||||||
|
- npmci command tsdoc
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- docker
|
||||||
- notpriv
|
- notpriv
|
||||||
@ -129,13 +117,3 @@ pages:
|
|||||||
paths:
|
paths:
|
||||||
- public
|
- public
|
||||||
allow_failure: true
|
allow_failure: true
|
||||||
|
|
||||||
windowsCompatibility:
|
|
||||||
image: stefanscherer/node-windows:10-build-tools
|
|
||||||
stage: metadata
|
|
||||||
script:
|
|
||||||
- npm install & npm test
|
|
||||||
coverage: /\d+.?\d+?\%\s*coverage/
|
|
||||||
tags:
|
|
||||||
- windows
|
|
||||||
allow_failure: true
|
|
||||||
|
56
README.md
56
README.md
@ -1,25 +1,23 @@
|
|||||||
# smartpromise
|
# @pushrocks/smartpromise
|
||||||
|
|
||||||
dropin replacement for q
|
simple promises and Deferred constructs
|
||||||
|
|
||||||
## Availabililty
|
## Availabililty and Links
|
||||||
|
|
||||||
[](https://www.npmjs.com/package/smartq)
|
- [npmjs.org (npm package)](https://www.npmjs.com/package/@pushrocks/smartpromise)
|
||||||
[](https://GitLab.com/pushrocks/smartq)
|
- [gitlab.com (source)](https://gitlab.com/pushrocks/smartpromise)
|
||||||
[](https://github.com/pushrocks/smartq)
|
- [github.com (source mirror)](https://github.com/pushrocks/smartpromise)
|
||||||
[](https://pushrocks.gitlab.io/smartq/)
|
- [docs (typedoc)](https://pushrocks.gitlab.io/smartpromise/)
|
||||||
|
|
||||||
## Status for master
|
## Status for master
|
||||||
|
|
||||||
[](https://GitLab.com/pushrocks/smartq/commits/master)
|
[](https://gitlab.com/pushrocks/smartpromise/commits/master)
|
||||||
[](https://GitLab.com/pushrocks/smartq/commits/master)
|
[](https://gitlab.com/pushrocks/smartpromise/commits/master)
|
||||||
[](https://www.npmjs.com/package/smartq)
|
[](https://www.npmjs.com/package/@pushrocks/smartpromise)
|
||||||
[](https://david-dm.org/pushrocks/smartq)
|
[](https://snyk.io/test/npm/@pushrocks/smartpromise)
|
||||||
[](https://www.bithound.io/github/pushrocks/smartq/master/dependencies/npm)
|
[](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
||||||
[](https://www.bithound.io/github/pushrocks/smartq)
|
[](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
||||||
[](https://nodejs.org/dist/latest-v6.x/docs/api/)
|
[](https://prettier.io/)
|
||||||
[](https://nodejs.org/dist/latest-v6.x/docs/api/)
|
|
||||||
[](http://standardjs.com/)
|
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
@ -28,17 +26,24 @@ Use TypeScript for best in class instellisense.
|
|||||||
> Note: smartq uses native ES6 promises
|
> Note: smartq uses native ES6 promises
|
||||||
> smartq does not repeat any native functions, so for things like .all() simply use Promise.all()
|
> smartq does not repeat any native functions, so for things like .all() simply use Promise.all()
|
||||||
|
|
||||||
```javascript
|
```typescript
|
||||||
import * as q from 'smartq'
|
import * as q from '@pushrocks/smartpromise'
|
||||||
|
|
||||||
// Deferred
|
// Deferred
|
||||||
// -----------------------------------------------
|
// -----------------------------------------------
|
||||||
let myAsyncFunction = (): Promise<string> => {
|
let myAsyncFunction = (): Promise<string> => {
|
||||||
let done = q.defer<string>() // returns your typical Deferred object
|
let done = q.defer<string>() // returns your typical Deferred object
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
done.resolve('hi') // will throw type error for other types than string as argument ;)
|
done.resolve('hi') // will throw type error for other types than string as argument ;)
|
||||||
},6000)
|
},6000);
|
||||||
return done.promise
|
|
||||||
|
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 myAsyncFunction2 = async () => {
|
||||||
@ -77,8 +82,9 @@ myPromisedFunction('helloThere', 2).then(x => {
|
|||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
For further information read the linked docs at the top of this README.
|
For further information read the linked docs at the top of this readme.
|
||||||
|
|
||||||
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
|
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
|
||||||
|
> | By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy)
|
||||||
|
|
||||||
[](https://push.rocks)
|
[](https://maintainedby.lossless.com)
|
||||||
|
@ -1,8 +1,16 @@
|
|||||||
{
|
{
|
||||||
"npmci": {
|
"npmci": {
|
||||||
"npmGlobalTools": [
|
"npmGlobalTools": [],
|
||||||
"npmts"
|
|
||||||
],
|
|
||||||
"npmAccessLevel": "public"
|
"npmAccessLevel": "public"
|
||||||
|
},
|
||||||
|
"gitzone": {
|
||||||
|
"module": {
|
||||||
|
"githost": "gitlab.com",
|
||||||
|
"gitscope": "pushrocks",
|
||||||
|
"gitrepo": "smartpromise",
|
||||||
|
"shortDescription": "simple promises and Deferred constructs",
|
||||||
|
"npmPackagename": "@pushrocks/smartpromise",
|
||||||
|
"license": "MIT"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
1481
package-lock.json
generated
1481
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
32
package.json
32
package.json
@ -1,13 +1,13 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartpromise",
|
"name": "@pushrocks/smartpromise",
|
||||||
"private": false,
|
"private": false,
|
||||||
"version": "2.0.2",
|
"version": "3.0.4",
|
||||||
"description": "dropin replacement for q",
|
"description": "simple promises and Deferred constructs",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"typings": "dist/index.d.ts",
|
"typings": "dist/index.d.ts",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "(tsrun test/test.ts)",
|
"test": "(tstest test/)",
|
||||||
"build": "(npmts)"
|
"build": "(tsbuild)"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
@ -19,12 +19,24 @@
|
|||||||
"url": "https://gitlab.com/pushrocks/smartq/issues"
|
"url": "https://gitlab.com/pushrocks/smartq/issues"
|
||||||
},
|
},
|
||||||
"homepage": "https://gitlab.com/pushrocks/smartq#README",
|
"homepage": "https://gitlab.com/pushrocks/smartq#README",
|
||||||
"dependencies": {
|
"dependencies": {},
|
||||||
"util.promisify": "^1.0.0"
|
|
||||||
},
|
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@gitzone/tsrun": "^1.0.18",
|
"@gitzone/tsbuild": "^2.1.17",
|
||||||
|
"@gitzone/tstest": "^1.0.24",
|
||||||
|
"@pushrocks/tapbundle": "^3.0.13",
|
||||||
"@types/node": "^10.5.1",
|
"@types/node": "^10.5.1",
|
||||||
"tapbundle": "^2.0.0"
|
"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"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
20
test/test.ts
20
test/test.ts
@ -1,10 +1,6 @@
|
|||||||
import { expect, tap } from 'tapbundle';
|
import { expect, tap } from '@pushrocks/tapbundle';
|
||||||
import * as q from '../ts/index';
|
import * as q from '../ts/index';
|
||||||
|
|
||||||
let myCallback = (someValue1: string, cb?) => {
|
|
||||||
cb(null, someValue1);
|
|
||||||
};
|
|
||||||
|
|
||||||
tap.test('should return a Deferred for .defer()', async () => {
|
tap.test('should return a Deferred for .defer()', async () => {
|
||||||
let myDeferred = q.defer();
|
let myDeferred = q.defer();
|
||||||
let expectPromise = expect(myDeferred.promise).to.eventually.be.fulfilled;
|
let expectPromise = expect(myDeferred.promise).to.eventually.be.fulfilled;
|
||||||
@ -20,16 +16,14 @@ tap.test('should let types flow through the Promise', async () => {
|
|||||||
return expectPromise;
|
return expectPromise;
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.test('should promisify a callback', async () => {
|
|
||||||
let myPromisified = q.promisify(myCallback);
|
|
||||||
let expectPromise = expect(myPromisified('hi')).to.eventually.equal('hi');
|
|
||||||
return await expectPromise;
|
|
||||||
});
|
|
||||||
|
|
||||||
tap.test('should map callbacks', async () => {
|
tap.test('should map callbacks', async () => {
|
||||||
let inputArray = ['hi', 'awesome'];
|
let inputArray = ['hi', 'awesome'];
|
||||||
let myPromisified = q.promisify(myCallback);
|
const myPromisified = async myInput => {
|
||||||
let expectPromise = expect(q.map(inputArray, myPromisified)).to.eventually.deep.equal(inputArray);
|
return myInput;
|
||||||
|
};
|
||||||
|
const expectPromise = expect(q.map(inputArray, myPromisified)).to.eventually.deep.equal(
|
||||||
|
inputArray
|
||||||
|
);
|
||||||
return expectPromise;
|
return expectPromise;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
49
ts/index.ts
49
ts/index.ts
@ -1,7 +1,5 @@
|
|||||||
import * as util from 'util';
|
|
||||||
|
|
||||||
export interface IResolve<T> {
|
export interface IResolve<T> {
|
||||||
(value?: T | Promise<T>): void;
|
(value?: T | PromiseLike<T>): void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface IReject {
|
export interface IReject {
|
||||||
@ -11,14 +9,34 @@ export interface IReject {
|
|||||||
export type TDeferredStatus = 'pending' | 'fulfilled' | 'rejected';
|
export type TDeferredStatus = 'pending' | 'fulfilled' | 'rejected';
|
||||||
|
|
||||||
export class Deferred<T> {
|
export class Deferred<T> {
|
||||||
promise: Promise<T>;
|
public promise: Promise<T>;
|
||||||
resolve: IResolve<T>;
|
public resolve: IResolve<T>;
|
||||||
reject: IReject;
|
public reject: IReject;
|
||||||
status: TDeferredStatus;
|
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() {
|
constructor() {
|
||||||
this.promise = new Promise<T>((resolve, reject) => {
|
this.promise = new Promise<T>((resolve, reject) => {
|
||||||
this.resolve = resolve;
|
this.resolve = (valueArg: T | PromiseLike<T>) => {
|
||||||
this.reject = reject;
|
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';
|
this.status = 'pending';
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -42,18 +60,15 @@ export let rejectedPromise = err => {
|
|||||||
return Promise.reject(err);
|
return Promise.reject(err);
|
||||||
};
|
};
|
||||||
|
|
||||||
export let promisify = util.promisify;
|
interface IAsyncFunction<T> {
|
||||||
|
(someArg: T): Promise<T>;
|
||||||
// polyfill
|
|
||||||
if (!promisify) {
|
|
||||||
promisify = require('util.promisify');
|
|
||||||
}
|
}
|
||||||
export let map = async <T>(inputArg: T[], functionArg) => {
|
|
||||||
let promisifedFunction = promisify(functionArg);
|
export let map = async <T>(inputArg: T[], functionArg: IAsyncFunction<T>) => {
|
||||||
let promiseArray: Promise<any>[] = [];
|
let promiseArray: Promise<any>[] = [];
|
||||||
let resultArray = [];
|
let resultArray = [];
|
||||||
for (let item of inputArg) {
|
for (let item of inputArg) {
|
||||||
let promise: Promise<any> = promisifedFunction(item);
|
let promise: Promise<any> = functionArg(item);
|
||||||
promiseArray.push(promise);
|
promiseArray.push(promise);
|
||||||
promise.then(x => {
|
promise.then(x => {
|
||||||
resultArray.push(x);
|
resultArray.push(x);
|
||||||
|
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