Compare commits
34 Commits
Author | SHA1 | Date | |
---|---|---|---|
1363aa7957 | |||
095ea36e5d | |||
b6ab079de0 | |||
734d5aca35 | |||
3b0052602f | |||
9693ec04d6 | |||
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 |
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
|
148
.gitlab-ci.yml
148
.gitlab-ci.yml
@ -1,16 +1,16 @@
|
|||||||
# gitzone standard
|
# gitzone ci_default
|
||||||
image: hosttoday/ht-docker-node:npmci
|
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||||
|
|
||||||
cache:
|
cache:
|
||||||
paths:
|
paths:
|
||||||
- .npmci_cache/
|
- .npmci_cache/
|
||||||
key: "$CI_BUILD_STAGE"
|
key: '$CI_BUILD_STAGE'
|
||||||
|
|
||||||
stages:
|
stages:
|
||||||
- security
|
- security
|
||||||
- test
|
- test
|
||||||
- release
|
- release
|
||||||
- metadata
|
- metadata
|
||||||
|
|
||||||
# ====================
|
# ====================
|
||||||
# security stage
|
# security stage
|
||||||
@ -18,108 +18,114 @@ stages:
|
|||||||
mirror:
|
mirror:
|
||||||
stage: security
|
stage: security
|
||||||
script:
|
script:
|
||||||
- npmci git mirror
|
- npmci git mirror
|
||||||
|
only:
|
||||||
|
- tags
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- lossless
|
||||||
- notpriv
|
- docker
|
||||||
|
- notpriv
|
||||||
|
|
||||||
snyk:
|
auditProductionDependencies:
|
||||||
|
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||||
stage: security
|
stage: security
|
||||||
script:
|
script:
|
||||||
- npmci command npm install -g snyk
|
- npmci npm prepare
|
||||||
- npmci command npm install --ignore-scripts
|
- npmci command npm install --production --ignore-scripts
|
||||||
- npmci command snyk test
|
- npmci command npm config set registry https://registry.npmjs.org
|
||||||
|
- npmci command npm audit --audit-level=high --only=prod --production
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- docker
|
||||||
- notpriv
|
|
||||||
|
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:
|
||||||
|
- docker
|
||||||
|
allow_failure: true
|
||||||
|
|
||||||
# ====================
|
# ====================
|
||||||
# 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
|
stage: test
|
||||||
script:
|
script:
|
||||||
- npmci node install lts
|
- npmci npm prepare
|
||||||
- npmci npm install
|
- npmci node install stable
|
||||||
- npmci npm test
|
- npmci npm install
|
||||||
|
- npmci npm test
|
||||||
coverage: /\d+.?\d+?\%\s*coverage/
|
coverage: /\d+.?\d+?\%\s*coverage/
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- docker
|
||||||
- notpriv
|
|
||||||
|
|
||||||
testSTABLE:
|
testBuild:
|
||||||
stage: test
|
stage: test
|
||||||
script:
|
script:
|
||||||
- npmci node install stable
|
- npmci npm prepare
|
||||||
- npmci npm install
|
- npmci node install stable
|
||||||
- npmci npm test
|
- npmci npm install
|
||||||
|
- npmci command npm run build
|
||||||
coverage: /\d+.?\d+?\%\s*coverage/
|
coverage: /\d+.?\d+?\%\s*coverage/
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- docker
|
||||||
- notpriv
|
|
||||||
|
|
||||||
release:
|
release:
|
||||||
stage: release
|
stage: release
|
||||||
script:
|
script:
|
||||||
- npmci node install stable
|
- npmci node install stable
|
||||||
- npmci npm publish
|
- npmci npm publish
|
||||||
only:
|
only:
|
||||||
- tags
|
- tags
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- lossless
|
||||||
- notpriv
|
- docker
|
||||||
|
- notpriv
|
||||||
|
|
||||||
# ====================
|
# ====================
|
||||||
# metadata stage
|
# metadata stage
|
||||||
# ====================
|
# ====================
|
||||||
codequality:
|
codequality:
|
||||||
stage: metadata
|
stage: metadata
|
||||||
image: docker:stable
|
|
||||||
allow_failure: true
|
allow_failure: true
|
||||||
services:
|
only:
|
||||||
- docker:stable-dind
|
- tags
|
||||||
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 prepare
|
||||||
--env SOURCE_CODE="$PWD"
|
- npmci npm install
|
||||||
--volume "$PWD":/code
|
- npmci command "tslint -c tslint.json ./ts/**/*.ts"
|
||||||
--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
|
- lossless
|
||||||
- priv
|
- docker
|
||||||
|
- priv
|
||||||
|
|
||||||
trigger:
|
trigger:
|
||||||
stage: metadata
|
stage: metadata
|
||||||
script:
|
script:
|
||||||
- npmci trigger
|
- npmci trigger
|
||||||
only:
|
only:
|
||||||
- tags
|
- tags
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- lossless
|
||||||
- notpriv
|
- docker
|
||||||
|
- notpriv
|
||||||
|
|
||||||
pages:
|
pages:
|
||||||
image: hosttoday/ht-docker-node:npmci
|
|
||||||
stage: metadata
|
stage: metadata
|
||||||
script:
|
script:
|
||||||
- npmci command npm install -g npmpage
|
- npmci node install lts
|
||||||
- npmci command npmpage
|
- npmci command npm install -g @gitzone/tsdoc
|
||||||
|
- npmci npm prepare
|
||||||
|
- npmci npm install
|
||||||
|
- npmci command tsdoc
|
||||||
tags:
|
tags:
|
||||||
|
- lossless
|
||||||
- docker
|
- docker
|
||||||
- notpriv
|
- notpriv
|
||||||
only:
|
only:
|
||||||
@ -127,15 +133,5 @@ pages:
|
|||||||
artifacts:
|
artifacts:
|
||||||
expire_in: 1 week
|
expire_in: 1 week
|
||||||
paths:
|
paths:
|
||||||
- public
|
- public
|
||||||
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
|
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"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
84
README.md
84
README.md
@ -1,84 +0,0 @@
|
|||||||
# smartpromise
|
|
||||||
|
|
||||||
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
|
|
||||||
})
|
|
||||||
```
|
|
||||||
|
|
||||||
For further information read the linked docs at the top of this README.
|
|
||||||
|
|
||||||
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
|
|
||||||
|
|
||||||
[](https://push.rocks)
|
|
@ -1,8 +1,17 @@
|
|||||||
{
|
{
|
||||||
"npmci": {
|
"npmci": {
|
||||||
"npmGlobalTools": [
|
"npmGlobalTools": [],
|
||||||
"npmts"
|
|
||||||
],
|
|
||||||
"npmAccessLevel": "public"
|
"npmAccessLevel": "public"
|
||||||
|
},
|
||||||
|
"gitzone": {
|
||||||
|
"projectType": "npm",
|
||||||
|
"module": {
|
||||||
|
"githost": "gitlab.com",
|
||||||
|
"gitscope": "pushrocks",
|
||||||
|
"gitrepo": "smartpromise",
|
||||||
|
"shortDescription": "simple promises and Deferred constructs",
|
||||||
|
"npmPackagename": "@pushrocks/smartpromise",
|
||||||
|
"license": "MIT"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
11107
package-lock.json
generated
11107
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
42
package.json
42
package.json
@ -1,13 +1,13 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartpromise",
|
"name": "@pushrocks/smartpromise",
|
||||||
"private": false,
|
"private": false,
|
||||||
"version": "2.0.2",
|
"version": "3.1.3",
|
||||||
"description": "dropin replacement for q",
|
"description": "simple promises and Deferred constructs",
|
||||||
"main": "dist/index.js",
|
"main": "dist_ts/index.js",
|
||||||
"typings": "dist/index.d.ts",
|
"typings": "dist_ts/index.d.ts",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "(tsrun test/test.ts)",
|
"test": "(tstest test/ --web)",
|
||||||
"build": "(npmts)"
|
"build": "(tsbuild --web)"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
@ -19,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": {},
|
||||||
"util.promisify": "^1.0.0"
|
|
||||||
},
|
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@gitzone/tsrun": "^1.0.18",
|
"@gitzone/tsbuild": "^2.1.25",
|
||||||
"@types/node": "^10.5.1",
|
"@gitzone/tstest": "^1.0.52",
|
||||||
"tapbundle": "^2.0.0"
|
"@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)
|
30
test/test.both.ts
Normal file
30
test/test.both.ts
Normal 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();
|
36
test/test.ts
36
test/test.ts
@ -1,36 +0,0 @@
|
|||||||
import { expect, tap } from 'tapbundle';
|
|
||||||
import * as q from '../ts/index';
|
|
||||||
|
|
||||||
let myCallback = (someValue1: string, cb?) => {
|
|
||||||
cb(null, someValue1);
|
|
||||||
};
|
|
||||||
|
|
||||||
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;
|
|
||||||
});
|
|
||||||
|
|
||||||
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;
|
|
||||||
});
|
|
||||||
|
|
||||||
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 () => {
|
|
||||||
let inputArray = ['hi', 'awesome'];
|
|
||||||
let myPromisified = q.promisify(myCallback);
|
|
||||||
let expectPromise = expect(q.map(inputArray, myPromisified)).to.eventually.deep.equal(inputArray);
|
|
||||||
return expectPromise;
|
|
||||||
});
|
|
||||||
|
|
||||||
tap.start();
|
|
78
ts/index.ts
78
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,54 +9,86 @@ 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';
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
||||||
};
|
};
|
||||||
|
|
||||||
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);
|
/**
|
||||||
let promiseArray: Promise<any>[] = [];
|
* accepts an array of inputs and a function that accepts the input.
|
||||||
let resultArray = [];
|
* runs all items with the function and returns the result array when all items have run
|
||||||
for (let item of inputArg) {
|
* @param inputArg
|
||||||
let promise: Promise<any> = promisifedFunction(item);
|
* @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);
|
promiseArray.push(promise);
|
||||||
promise.then(x => {
|
promise.then((x) => {
|
||||||
resultArray.push(x);
|
resultArray.push(x);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
await Promise.all(promiseArray);
|
await Promise.all(promiseArray);
|
||||||
return resultArray;
|
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);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
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