Compare commits
49 Commits
Author | SHA1 | Date | |
---|---|---|---|
4ee932873c | |||
3bedd3581e | |||
ee3c1c6e79 | |||
8c6ac181b0 | |||
51bce0b317 | |||
374d73f87e | |||
d922c691ed | |||
5aecc44ad7 | |||
f7492c4656 | |||
63dd46ed49 | |||
984c2bc9d8 | |||
16030d5ee8 | |||
ddce50ac0f | |||
baf956d0ed | |||
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 |
23
.gitignore
vendored
23
.gitignore
vendored
@ -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
|
114
.gitlab-ci.yml
114
.gitlab-ci.yml
@ -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
29
.vscode/launch.json
vendored
Normal 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
26
.vscode/settings.json
vendored
Normal 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"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
86
README.md
86
README.md
@ -1,86 +0,0 @@
|
|||||||
# smartq
|
|
||||||
dropin replacement for q
|
|
||||||
|
|
||||||
## Availabililty
|
|
||||||
[](https://www.npmjs.com/package/smartq)
|
|
||||||
[](https://GitLab.com/pushrocks/smartq)
|
|
||||||
[](https://github.com/pushrocks/smartq)
|
|
||||||
[](https://pushrocks.gitlab.io/smartq/)
|
|
||||||
|
|
||||||
## 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/)
|
|
||||||
|
|
||||||
## 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'
|
|
||||||
|
|
||||||
// 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)
|
|
||||||
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
|
|
||||||
})
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
## Dependency Credits:
|
|
||||||
who | made what
|
|
||||||
-- | --
|
|
||||||
[notenoughneon](https://www.npmjs.com/~notenoughneon) | [typed-promisify](https://www.npmjs.com/package/typed-promisify)
|
|
||||||
|
|
||||||
For further information read the linked docs at the top of this README.
|
|
||||||
|
|
||||||
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
|
|
||||||
|
|
||||||
[](https://push.rocks)
|
|
25
dist/index.d.ts
vendored
25
dist/index.d.ts
vendored
@ -1,25 +0,0 @@
|
|||||||
import 'typings-global';
|
|
||||||
import * as typedPromisify from 'typed-promisify';
|
|
||||||
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 new resolved promise for the provided value.
|
|
||||||
*/
|
|
||||||
export declare let resolvedPromise: <T>(value?: T) => Promise<T>;
|
|
||||||
/**
|
|
||||||
* Creates a new rejected promise for the provided reason.
|
|
||||||
*/
|
|
||||||
export declare let rejectedPromise: (err: any) => Promise<never>;
|
|
||||||
export declare let promisify: typeof typedPromisify.promisify;
|
|
||||||
export declare let map: typeof typedPromisify.map;
|
|
30
dist/index.js
vendored
30
dist/index.js
vendored
@ -1,30 +0,0 @@
|
|||||||
"use strict";
|
|
||||||
require("typings-global");
|
|
||||||
const typedPromisify = require("typed-promisify");
|
|
||||||
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 new resolved promise for the provided value.
|
|
||||||
*/
|
|
||||||
exports.resolvedPromise = (value) => {
|
|
||||||
return Promise.resolve(value);
|
|
||||||
};
|
|
||||||
/**
|
|
||||||
* Creates a new rejected promise for the provided reason.
|
|
||||||
*/
|
|
||||||
exports.rejectedPromise = (err) => {
|
|
||||||
return Promise.reject(err);
|
|
||||||
};
|
|
||||||
exports.promisify = typedPromisify.promisify;
|
|
||||||
exports.map = typedPromisify.map;
|
|
||||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJz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
},
|
||||||
|
"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
11064
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
44
package.json
44
package.json
@ -1,11 +1,13 @@
|
|||||||
{
|
{
|
||||||
"name": "smartq",
|
"name": "@pushrocks/smartpromise",
|
||||||
"version": "1.1.1",
|
"private": false,
|
||||||
"description": "dropin replacement for q",
|
"version": "3.1.0",
|
||||||
"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/)",
|
||||||
|
"build": "(tsbuild)"
|
||||||
},
|
},
|
||||||
"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": {},
|
||||||
"typed-promisify": "^0.3.0",
|
|
||||||
"typings-global": "^1.0.14"
|
|
||||||
},
|
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"smartchai": "^1.0.1",
|
"@gitzone/tsbuild": "^2.1.25",
|
||||||
"typings-test": "^1.0.3"
|
"@gitzone/tstest": "^1.0.52",
|
||||||
}
|
"@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"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
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)
|
1
test/test.d.ts
vendored
1
test/test.d.ts
vendored
@ -1 +0,0 @@
|
|||||||
import 'typings-test';
|
|
34
test/test.js
34
test/test.js
@ -1,34 +0,0 @@
|
|||||||
"use strict";
|
|
||||||
require("typings-test");
|
|
||||||
const smartchai_1 = require("smartchai");
|
|
||||||
const q = require("../dist/index");
|
|
||||||
let myCallback = (someValue1, cb) => {
|
|
||||||
cb(null, someValue1);
|
|
||||||
};
|
|
||||||
describe('smartq', function () {
|
|
||||||
it('should return a Deferred for .defer()', function () {
|
|
||||||
let myDeferred = q.defer();
|
|
||||||
let expectPromise = smartchai_1.expect(myDeferred.promise).to.eventually.be.fulfilled;
|
|
||||||
myDeferred.resolve();
|
|
||||||
return expectPromise;
|
|
||||||
});
|
|
||||||
it('should let types flow through the Promise', function () {
|
|
||||||
let myString = 'someString';
|
|
||||||
let myDeferred = q.defer();
|
|
||||||
let expectPromise = smartchai_1.expect(myDeferred.promise).to.eventually.equal('someString');
|
|
||||||
myDeferred.resolve(myString);
|
|
||||||
return expectPromise;
|
|
||||||
});
|
|
||||||
it('should promisify a callback', function () {
|
|
||||||
let myPromisified = q.promisify(myCallback);
|
|
||||||
let expectPromise = smartchai_1.expect(myPromisified('hi')).to.eventually.equal('hi');
|
|
||||||
return expectPromise;
|
|
||||||
});
|
|
||||||
it('should map callbacks', function () {
|
|
||||||
let inputArray = ['hi', 'awesome'];
|
|
||||||
let myPromisified = q.promisify(myCallback);
|
|
||||||
let expectPromise = smartchai_1.expect(q.map(inputArray, myPromisified)).to.eventually.deep.equal(inputArray);
|
|
||||||
return expectPromise;
|
|
||||||
});
|
|
||||||
});
|
|
||||||
//# sourceMappingURL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DQUFBO0lBQ3hCLENBQUMsQ0FBQyxDQUFBO0lBRUYsRUFBRSxDQUFDLDZCQUE2QixFQUFFO1FBQzlCLElBQUksYUFBYSxHQUFHLENBQUMsQ0FBQyxTQUFTLENBQUMsVUFBVSxDQUFDLENBQUE7UUFDM0MsSUFBSSxhQUFhLEdBQUcsa0JBQU0sQ0FBQyxhQUFhLENBQUMsSUFBSSxDQUFDLENBQUMsQ0FBQyxFQUFFLENBQUMsVUFBVSxDQUFDLEtBQUssQ0FBQyxJQUFJLENBQUMsQ0FBQTtRQUN6RSxNQUFNLENBQUMsYUFBYSxDQUFBO0lBQ3hCLENBQUMsQ0FBQyxDQUFBO0lBRUYsRUFBRSxDQUFDLHNCQUFzQixFQUFFO1FBQ3ZCLElBQUksVUFBVSxHQUFHLENBQUMsSUFBSSxFQUFFLFNBQVMsQ0FBQyxDQUFBO1FBQ2xDLElBQUksYUFBYSxHQUFHLENBQUMsQ0FBQyxTQUFTLENBQUMsVUFBVSxDQUFDLENBQUE7UUFDM0MsSUFBSSxhQUFhLEdBQUcsa0JBQU0sQ0FBQyxDQUFDLENBQUMsR0FBRyxDQUFDLFVBQVUsRUFBRSxhQUFhLENBQUMsQ0FBQyxDQUFDLEVBQUUsQ0FBQyxVQUFVLENBQUMsSUFBSSxDQUFDLEtBQUssQ0FBQyxVQUFVLENBQUMsQ0FBQTtRQUNqRyxNQUFNLENBQUMsYUFBYSxDQUFBO0lBQ3hCLENBQUMsQ0FBQyxDQUFBO0FBQ04sQ0FBQyxDQUFDLENBQUEifQ==
|
|
60
test/test.ts
60
test/test.ts
@ -1,38 +1,30 @@
|
|||||||
import 'typings-test'
|
import { expect, tap } from '@pushrocks/tapbundle';
|
||||||
|
import * as smartpromise from '../ts/index';
|
||||||
|
|
||||||
import { expect } from 'smartchai'
|
tap.test('should return a Deferred for .defer()', async () => {
|
||||||
import * as q from '../dist/index'
|
const myDeferred = smartpromise.defer();
|
||||||
|
const expectPromise = expect(myDeferred.promise).to.eventually.be.fulfilled;
|
||||||
|
myDeferred.resolve();
|
||||||
|
return expectPromise;
|
||||||
|
});
|
||||||
|
|
||||||
let myCallback = (someValue1: string, cb?) => {
|
tap.test('should let types flow through the Promise', async () => {
|
||||||
cb(null, someValue1)
|
const myString = 'someString';
|
||||||
}
|
const myDeferred = smartpromise.defer<string>();
|
||||||
|
const expectPromise = expect(myDeferred.promise).to.eventually.equal('someString');
|
||||||
|
myDeferred.resolve(myString);
|
||||||
|
return expectPromise;
|
||||||
|
});
|
||||||
|
|
||||||
describe('smartq', function () {
|
tap.test('should map callbacks', async () => {
|
||||||
it('should return a Deferred for .defer()', function () {
|
const inputArray = ['hi', 'awesome'];
|
||||||
let myDeferred = q.defer()
|
const myPromisified = async (myInput) => {
|
||||||
let expectPromise = expect(myDeferred.promise).to.eventually.be.fulfilled
|
return myInput;
|
||||||
myDeferred.resolve()
|
};
|
||||||
return expectPromise
|
const expectPromise = expect(
|
||||||
})
|
smartpromise.map(inputArray, myPromisified)
|
||||||
|
).to.eventually.deep.equal(inputArray);
|
||||||
|
return expectPromise;
|
||||||
|
});
|
||||||
|
|
||||||
it('should let types flow through the Promise', function () {
|
tap.start();
|
||||||
let myString = 'someString'
|
|
||||||
let myDeferred = q.defer<string>()
|
|
||||||
let expectPromise = expect(myDeferred.promise).to.eventually.equal('someString')
|
|
||||||
myDeferred.resolve(myString)
|
|
||||||
return expectPromise
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should promisify a callback', function () {
|
|
||||||
let myPromisified = q.promisify(myCallback)
|
|
||||||
let expectPromise = expect(myPromisified('hi')).to.eventually.equal('hi')
|
|
||||||
return expectPromise
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should map callbacks', function() {
|
|
||||||
let inputArray = ['hi', 'awesome']
|
|
||||||
let myPromisified = q.promisify(myCallback)
|
|
||||||
let expectPromise = expect(q.map(inputArray, myPromisified)).to.eventually.deep.equal(inputArray)
|
|
||||||
return expectPromise
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
99
ts/index.ts
99
ts/index.ts
@ -1,45 +1,94 @@
|
|||||||
import 'typings-global'
|
|
||||||
|
|
||||||
import * as typedPromisify from 'typed-promisify'
|
|
||||||
|
|
||||||
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;
|
||||||
constructor() {
|
public status: TDeferredStatus;
|
||||||
this.promise = new Promise<T>((resolve, reject) => {
|
|
||||||
this.resolve = resolve
|
public startedAt: number;
|
||||||
this.reject = reject
|
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>() => {
|
export const defer = <T>() => {
|
||||||
return new Deferred<T>()
|
return new Deferred<T>();
|
||||||
}
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new resolved promise for the provided value.
|
* Creates a new resolved promise for the provided value.
|
||||||
*/
|
*/
|
||||||
export let resolvedPromise = <T>(value?: T): Promise<T> => {
|
export const resolvedPromise = <T>(value?: T): Promise<T> => {
|
||||||
return Promise.resolve(value)
|
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 = (err) => {
|
export const rejectedPromise = (err) => {
|
||||||
return Promise.reject(err)
|
return Promise.reject(err);
|
||||||
|
};
|
||||||
|
|
||||||
|
interface IAsyncFunction<T> {
|
||||||
|
(someArg: T): Promise<T>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export let promisify = typedPromisify.promisify
|
/**
|
||||||
export let map = typedPromisify.map
|
* accepts an array of inputs and a function that accepts the input.
|
||||||
export let _try = typedPromisify._try
|
* 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 = (ms, promise) => {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
setTimeout(() => {
|
||||||
|
reject(new Error('timeout'));
|
||||||
|
}, ms);
|
||||||
|
promise.then(resolve, reject);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
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