Compare commits

..

66 Commits

Author SHA1 Message Date
9f6d88e162 3.1.5 2021-04-23 18:04:09 +00:00
255d6415a5 fix(core): update 2021-04-23 18:04:08 +00:00
84a8d8431c 3.1.4 2021-04-23 17:58:37 +00:00
95d27d52a7 fix(core): update 2021-04-23 17:58:37 +00:00
1363aa7957 3.1.3 2020-10-16 02:06:50 +00:00
095ea36e5d fix(core): update 2020-10-16 02:06:50 +00:00
b6ab079de0 3.1.2 2020-10-16 01:39:19 +00:00
734d5aca35 fix(core): update 2020-10-16 01:39:19 +00:00
3b0052602f 3.1.1 2020-10-15 18:19:14 +00:00
9693ec04d6 fix(core): update 2020-10-15 18:19:12 +00:00
4ee932873c 3.1.0 2020-10-15 18:16:17 +00:00
3bedd3581e feat(timeouts): allows the wrapping of promises in timeouts 2020-10-15 18:16:17 +00:00
ee3c1c6e79 3.0.9 2020-10-15 18:14:53 +00:00
8c6ac181b0 fix(core): update 2020-10-15 18:14:53 +00:00
51bce0b317 3.0.8 2020-10-15 18:13:58 +00:00
374d73f87e fix(core): update 2020-10-15 18:13:58 +00:00
d922c691ed 3.0.7 2020-10-15 18:02:14 +00:00
5aecc44ad7 fix(core): update 2020-10-15 18:02:14 +00:00
f7492c4656 3.0.6 2019-10-01 18:06:29 +02:00
63dd46ed49 fix(core): update 2019-10-01 18:06:29 +02:00
984c2bc9d8 3.0.5 2019-09-11 16:44:21 +02:00
16030d5ee8 fix(core): update 2019-09-11 16:44:21 +02:00
ddce50ac0f 3.0.4 2019-09-11 15:38:37 +02:00
baf956d0ed fix(core): update 2019-09-11 15:38:37 +02:00
3340f1a895 3.0.3 2019-09-11 15:37:18 +02:00
ee336af699 fix(core): update 2019-09-11 15:37:17 +02:00
0526c45ea1 3.0.2 2019-03-26 12:11:25 +01:00
9933d72784 fix(core): update 2019-03-26 12:11:24 +01:00
d1bc689b35 3.0.1 2019-03-26 12:07:13 +01:00
25c0deb3f3 fix(core): update 2019-03-26 12:07:12 +01:00
5138219563 3.0.0 2019-03-26 12:02:02 +01:00
93f426cce0 BREAKING CHANGE(remove util dependency and promisify functionality): update 2019-03-26 12:02:01 +01:00
e42061047a 2.0.5 2018-07-03 08:41:43 +02:00
3b07f99488 fix(README): fix import statement 2018-07-03 08:41:42 +02:00
3d252a5e3e 2.0.4 2018-07-03 08:39:25 +02:00
d82722296b fix(README): update name 2018-07-03 08:39:25 +02:00
22317ef83d 2.0.3 2018-07-02 22:59:16 +02:00
054ec0afa4 fix(core): now has 0 dependencies 2018-07-02 22:59:16 +02:00
19d434533f 2.0.2 2018-07-02 22:54:14 +02:00
9b8440f743 fix(core): update to latest gitzone standards 2018-07-02 22:54:13 +02:00
564953e25f 2.0.1 2018-07-02 22:52:45 +02:00
98f190ea21 fix(test): adjust windows test 2018-07-02 22:52:45 +02:00
2b77c8e019 2.0.0 2018-07-02 22:49:21 +02:00
d8e5fbf3bc BREAKING CHANGE(scope): switch scope to pushrocks 2018-07-02 22:49:21 +02:00
1069d5a38e 1.1.8 2018-03-16 10:24:30 +01:00
eb82d16859 update to latest standards 2018-03-16 10:24:27 +01:00
082f533975 update to latest standards 2018-03-16 10:24:12 +01:00
54b2fb0ae5 1.1.7 2017-07-27 18:17:51 +02:00
31c7e607cc start with promise status implementation 2017-07-27 18:17:37 +02:00
b15cdb48a3 1.1.6 2017-07-06 15:00:16 +02:00
7c1605eccf update README and add docs 2017-07-06 15:00:13 +02:00
cea619e964 1.1.5 2017-07-06 14:55:52 +02:00
fb866b36af add polyfill 2017-07-06 14:55:42 +02:00
a8934950ef 1.1.4 2017-07-06 14:14:16 +02:00
590dc27d20 add npmextra.json 2017-07-06 14:14:11 +02:00
3bcff82d31 1.1.3 2017-07-06 14:12:38 +02:00
bfbea0acde update ci 2017-07-06 14:12:35 +02:00
ba18b31f2f 1.1.2 2017-07-06 14:11:24 +02:00
4fc619970f switch to native promisify 2017-07-06 14:10:18 +02:00
e0cfa6ca29 1.1.1 2017-02-12 12:56:29 +01:00
843d217698 update README 2017-02-12 12:56:22 +01:00
dbd2fe73b8 1.1.0 2017-01-28 16:37:58 +01:00
1eea124cf4 add promisify 2017-01-28 16:37:54 +01:00
6a4ee22f36 1.0.4 2017-01-21 00:45:14 +01:00
e23f946e50 improve upon smartq 2017-01-21 00:45:11 +01:00
8d62fc6ef1 improve README 2017-01-20 22:01:10 +01:00
17 changed files with 11518 additions and 271 deletions

23
.gitignore vendored
View File

@ -1,5 +1,20 @@
coverage/ .nogit/
pages/
node_modules/
public/
# artifacts
coverage/
public/
pages/
# installs
node_modules/
# caches
.yarn/
.cache/
.rpt2_cache
# builds
dist/
dist_*/
# custom

View File

@ -1,59 +1,137 @@
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: stages:
- test - security
- release - test
- trigger - release
- pages - metadata
testLEGACY: # ====================
stage: test # security stage
# ====================
mirror:
stage: security
script: script:
- npmci test legacy - npmci git mirror
only:
- tags
tags:
- lossless
- docker
- notpriv
auditProductionDependencies:
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
stage: security
script:
- npmci npm prepare
- npmci command npm install --production --ignore-scripts
- npmci command npm config set registry https://registry.npmjs.org
- npmci command npm audit --audit-level=high --only=prod --production
tags:
- docker
auditDevDependencies:
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
stage: security
script:
- npmci npm prepare
- npmci command npm install --ignore-scripts
- npmci command npm config set registry https://registry.npmjs.org
- npmci command npm audit --audit-level=high --only=dev
tags: tags:
- docker - docker
allow_failure: true allow_failure: true
testLTS: # ====================
# test stage
# ====================
testStable:
stage: test stage: test
script: script:
- npmci test lts - npmci npm prepare
- npmci node install stable
- npmci npm install
- npmci npm test
coverage: /\d+.?\d+?\%\s*coverage/
tags: tags:
- docker - docker
testSTABLE: testBuild:
stage: test stage: test
script: 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: tags:
- docker - docker
release: release:
stage: release stage: release
script: script:
- npmci publish - npmci node install stable
- npmci npm publish
only: only:
- tags - tags
tags: tags:
- lossless
- docker - docker
- notpriv
# ====================
# metadata stage
# ====================
codequality:
stage: metadata
allow_failure: true
only:
- tags
script:
- npmci command npm install -g tslint typescript
- npmci npm prepare
- npmci npm install
- npmci command "tslint -c tslint.json ./ts/**/*.ts"
tags:
- lossless
- docker
- priv
trigger: trigger:
stage: trigger stage: metadata
script: script:
- npmci trigger - npmci trigger
only: only:
- tags - tags
tags: tags:
- lossless
- docker - docker
- notpriv
pages: pages:
image: hosttoday/ht-docker-node:npmpage stage: metadata
stage: pages
script: script:
- npmci command npmpage --host gitlab - npmci node install lts
- npmci command npm install -g @gitzone/tsdoc
- npmci npm prepare
- npmci npm install
- npmci command tsdoc
tags:
- lossless
- docker
- notpriv
only: only:
- tags - tags
artifacts: artifacts:
expire_in: 1 week expire_in: 1 week
paths: paths:
- public - public
allow_failure: true

29
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,29 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "current file",
"type": "node",
"request": "launch",
"args": [
"${relativeFile}"
],
"runtimeArgs": ["-r", "@gitzone/tsrun"],
"cwd": "${workspaceRoot}",
"protocol": "inspector",
"internalConsoleOptions": "openOnSessionStart"
},
{
"name": "test.ts",
"type": "node",
"request": "launch",
"args": [
"test/test.ts"
],
"runtimeArgs": ["-r", "@gitzone/tsrun"],
"cwd": "${workspaceRoot}",
"protocol": "inspector",
"internalConsoleOptions": "openOnSessionStart"
}
]
}

26
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,26 @@
{
"json.schemas": [
{
"fileMatch": ["/npmextra.json"],
"schema": {
"type": "object",
"properties": {
"npmci": {
"type": "object",
"description": "settings for npmci"
},
"gitzone": {
"type": "object",
"description": "settings for gitzone",
"properties": {
"projectType": {
"type": "string",
"enum": ["website", "element", "service", "npm", "wcc"]
}
}
}
}
}
}
]
}

View File

@ -1,68 +0,0 @@
# smartq
dropin replacement for q
## Availabililty
[![npm](https://push.rocks/assets/repo-button-npm.svg)](https://www.npmjs.com/package/smartq)
[![git](https://push.rocks/assets/repo-button-git.svg)](https://GitLab.com/pushrocks/smartq)
[![git](https://push.rocks/assets/repo-button-mirror.svg)](https://github.com/pushrocks/smartq)
[![docs](https://push.rocks/assets/repo-button-docs.svg)](https://pushrocks.gitlab.io/smartq/)
## 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.
> Note: smartq uses native ES6 promises
```javascript
import * as q from 'smartq'
let myAsyncFunction = (): Promise<string> => {
let done = q.defer() // returns your typical Deferred object
setTimeout(() => {
done.resolve('hi')
},6000)
return done.promise
}
let myAsyncFunction2 = async () => {
let aString = await myAsyncFunction()
console.log(aString) // will log 'hi' to console
}
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')
})
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)
})
```
[![npm](https://push.rocks/assets/repo-header.svg)](https://push.rocks)

60
dist/index.d.ts vendored
View File

@ -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
View File

@ -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=

17
npmextra.json Normal file
View File

@ -0,0 +1,17 @@
{
"npmci": {
"npmGlobalTools": [],
"npmAccessLevel": "public"
},
"gitzone": {
"projectType": "npm",
"module": {
"githost": "gitlab.com",
"gitscope": "pushrocks",
"gitrepo": "smartpromise",
"shortDescription": "simple promises and Deferred constructs",
"npmPackagename": "@pushrocks/smartpromise",
"license": "MIT"
}
}
}

11064
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,11 +1,13 @@
{ {
"name": "smartq", "name": "@pushrocks/smartpromise",
"version": "1.0.3", "private": false,
"description": "dropin replacement for q", "version": "3.1.5",
"main": "dist/index.js", "description": "simple promises and Deferred constructs",
"typings": "dist/index.d.ts", "main": "dist_ts/index.js",
"typings": "dist_ts/index.d.ts",
"scripts": { "scripts": {
"test": "(npmts)" "test": "(tstest test/ --web)",
"build": "(tsbuild --web)"
}, },
"repository": { "repository": {
"type": "git", "type": "git",
@ -17,12 +19,28 @@
"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": {},
"typings-global": "^1.0.14"
},
"devDependencies": { "devDependencies": {
"@types/should": "^8.1.30", "@gitzone/tsbuild": "^2.1.25",
"should": "^11.1.2", "@gitzone/tstest": "^1.0.52",
"typings-test": "^1.0.3" "@pushrocks/tapbundle": "^3.2.9",
} "@types/node": "^14.11.8",
"tslint": "^6.1.3",
"tslint-config-prettier": "^1.18.0"
},
"files": [
"ts/**/*",
"ts_web/**/*",
"dist/**/*",
"dist_*/**/*",
"dist_ts/**/*",
"dist_ts_web/**/*",
"assets/**/*",
"cli.js",
"npmextra.json",
"readme.md"
],
"browserslist": [
"last 1 chrome versions"
]
} }

99
readme.md Normal file
View File

@ -0,0 +1,99 @@
# @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
Status Category | Status Badge
-- | --
GitLab Pipelines | [![pipeline status](https://gitlab.com/pushrocks/smartpromise/badges/master/pipeline.svg)](https://lossless.cloud)
GitLab Pipline Test Coverage | [![coverage report](https://gitlab.com/pushrocks/smartpromise/badges/master/coverage.svg)](https://lossless.cloud)
npm | [![npm downloads per month](https://badgen.net/npm/dy/@pushrocks/smartpromise)](https://lossless.cloud)
Snyk | [![Known Vulnerabilities](https://badgen.net/snyk/pushrocks/smartpromise)](https://lossless.cloud)
TypeScript Support | [![TypeScript](https://badgen.net/badge/TypeScript/>=%203.x/blue?icon=typescript)](https://lossless.cloud)
node Support | [![node](https://img.shields.io/badge/node->=%2010.x.x-blue.svg)](https://nodejs.org/dist/latest-v10.x/docs/api/)
Code Style | [![Code Style](https://badgen.net/badge/style/prettier/purple)](https://lossless.cloud)
PackagePhobia (total standalone install weight) | [![PackagePhobia](https://badgen.net/packagephobia/install/@pushrocks/smartpromise)](https://lossless.cloud)
PackagePhobia (package size on registry) | [![PackagePhobia](https://badgen.net/packagephobia/publish/@pushrocks/smartpromise)](https://lossless.cloud)
BundlePhobia (total size when bundled) | [![BundlePhobia](https://badgen.net/bundlephobia/minzip/@pushrocks/smartpromise)](https://lossless.cloud)
Platform support | [![Supports Windows 10](https://badgen.net/badge/supports%20Windows%2010/yes/green?icon=windows)](https://lossless.cloud) [![Supports Mac OS X](https://badgen.net/badge/supports%20Mac%20OS%20X/yes/green?icon=apple)](https://lossless.cloud)
## 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
});
```
## Contribution
We are always happy for code contributions. If you are not the code contributing type that is ok. Still, maintaining Open Source repositories takes considerable time and thought. If you like the quality of what we do and our modules are useful to you we would appreciate a little monthly contribution: You can [contribute one time](https://lossless.link/contribute-onetime) or [contribute monthly](https://lossless.link/contribute). :)
For further information read the linked docs at the top of this readme.
> MIT licensed | **&copy;** [Lossless GmbH](https://lossless.gmbh)
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy)
[![repo-footer](https://lossless.gitlab.io/publicrelations/repofooter.svg)](https://maintainedby.lossless.com)

30
test/test.both.ts Normal file
View File

@ -0,0 +1,30 @@
import { expect, tap } from '@pushrocks/tapbundle';
import * as smartpromise from '../ts/index';
tap.test('should return a Deferred for .defer()', async () => {
const myDeferred = smartpromise.defer();
const expectPromise = expect(myDeferred.promise).to.eventually.be.fulfilled;
myDeferred.resolve();
return expectPromise;
});
tap.test('should let types flow through the Promise', async () => {
const myString = 'someString';
const myDeferred = smartpromise.defer<string>();
const expectPromise = expect(myDeferred.promise).to.eventually.equal('someString');
myDeferred.resolve(myString);
return expectPromise;
});
tap.test('should map callbacks', async () => {
const inputArray = ['hi', 'awesome'];
const myPromisified = async (myInput) => {
return myInput;
};
const expectPromise = expect(
smartpromise.map(inputArray, myPromisified)
).to.eventually.deep.equal(inputArray);
return expectPromise;
});
tap.start();

1
test/test.d.ts vendored
View File

@ -1 +0,0 @@
import 'typings-test';

View File

@ -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=

View File

@ -1,24 +0,0 @@
import 'typings-test'
import * as should from 'should'
import * as q from '../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<string>()
myDeferred.promise.then(x => {
should(x).equal('someString')
done()
})
myDeferred.resolve(myString)
})
})

View File

@ -1,45 +1,109 @@
import 'typings-global'
export interface IResolve<T> { export interface IResolve<T> {
(value?: T | Promise<T>): void (value?: T | PromiseLike<T>): void;
} }
export interface IReject { export interface IReject {
(reason?: any): void (reason?: any): void;
} }
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;
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';
});
} }
} }
export let defer = <T>() => { export const defer = <T>() => {
return new Deferred<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
/** /**
* Creates a new resolved promise for the provided value. * Creates a new resolved promise for the provided value.
*/ */
export let resolvedPromise = Promise.resolve export const resolvedPromise = <T>(value?: T): Promise<T> => {
return Promise.resolve(value);
};
/** /**
* Creates a new rejected promise for the provided reason. * Creates a new rejected promise for the provided reason.
*/ */
export let rejectedPromise = Promise.reject export const rejectedPromise = (err) => {
return Promise.reject(err);
};
interface IAsyncFunction<T> {
(someArg: T): Promise<T>;
}
/**
* accepts an array of inputs and a function that accepts the input.
* runs all items with the function and returns the result array when all items have run
* @param inputArg
* @param functionArg
*/
export const map = async <T>(inputArg: T[], functionArg: IAsyncFunction<T>) => {
const promiseArray: Promise<any>[] = [];
const resultArray = [];
for (const item of inputArg) {
const promise: Promise<any> = functionArg(item);
promiseArray.push(promise);
promise.then((x) => {
resultArray.push(x);
});
}
await Promise.all(promiseArray);
return resultArray;
};
export const timeoutWrap = <T = any>(promiseArg: Promise<T>, timeoutInMs: number) => {
return new Promise<T>((resolve, reject) => {
setTimeout(() => {
reject(new Error('timeout'));
}, timeoutInMs);
promiseArg.then(resolve, reject);
});
};
export const getFirstTrueOrFalse = async (promisesArg: Promise<boolean>[]) => {
const done = defer<boolean>();
for (const promiseArg of promisesArg) {
promiseArg.then((resultArg) => {
if (resultArg === true) {
done.resolve(true);
}
});
}
Promise.all(promisesArg).then(() => {
done.resolve(false);
});
return done.promise;
};

View File

@ -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"
} }