Compare commits
22 Commits
Author | SHA1 | Date | |
---|---|---|---|
40642fd6f6 | |||
4c3565c618 | |||
1848006601 | |||
c761cc4460 | |||
9f6d88e162 | |||
255d6415a5 | |||
84a8d8431c | |||
95d27d52a7 | |||
1363aa7957 | |||
095ea36e5d | |||
b6ab079de0 | |||
734d5aca35 | |||
3b0052602f | |||
9693ec04d6 | |||
4ee932873c | |||
3bedd3581e | |||
ee3c1c6e79 | |||
8c6ac181b0 | |||
51bce0b317 | |||
374d73f87e | |||
d922c691ed | |||
5aecc44ad7 |
4
.gitignore
vendored
4
.gitignore
vendored
@ -15,8 +15,6 @@ node_modules/
|
||||
|
||||
# builds
|
||||
dist/
|
||||
dist_web/
|
||||
dist_serve/
|
||||
dist_ts_web/
|
||||
dist_*/
|
||||
|
||||
# custom
|
@ -4,13 +4,13 @@ image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||
cache:
|
||||
paths:
|
||||
- .npmci_cache/
|
||||
key: "$CI_BUILD_STAGE"
|
||||
key: '$CI_BUILD_STAGE'
|
||||
|
||||
stages:
|
||||
- security
|
||||
- test
|
||||
- release
|
||||
- metadata
|
||||
- security
|
||||
- test
|
||||
- release
|
||||
- metadata
|
||||
|
||||
# ====================
|
||||
# security stage
|
||||
@ -19,20 +19,35 @@ mirror:
|
||||
stage: security
|
||||
script:
|
||||
- npmci git mirror
|
||||
only:
|
||||
- tags
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
snyk:
|
||||
auditProductionDependencies:
|
||||
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||
stage: security
|
||||
script:
|
||||
- npmci npm prepare
|
||||
- npmci command npm install -g snyk
|
||||
- npmci command npm install --ignore-scripts
|
||||
- npmci command snyk test
|
||||
- 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
|
||||
- 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
|
||||
@ -48,7 +63,6 @@ testStable:
|
||||
coverage: /\d+.?\d+?\%\s*coverage/
|
||||
tags:
|
||||
- docker
|
||||
- priv
|
||||
|
||||
testBuild:
|
||||
stage: test
|
||||
@ -60,7 +74,6 @@ testBuild:
|
||||
coverage: /\d+.?\d+?\%\s*coverage/
|
||||
tags:
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
release:
|
||||
stage: release
|
||||
@ -70,6 +83,7 @@ release:
|
||||
only:
|
||||
- tags
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
@ -79,11 +93,15 @@ release:
|
||||
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
|
||||
|
||||
@ -94,20 +112,20 @@ trigger:
|
||||
only:
|
||||
- tags
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
pages:
|
||||
image: hosttoday/ht-docker-dbase:npmci
|
||||
services:
|
||||
- docker:stable-dind
|
||||
stage: metadata
|
||||
script:
|
||||
- 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:
|
||||
|
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"]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
90
README.md
90
README.md
@ -1,90 +0,0 @@
|
||||
# @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)
|
@ -4,6 +4,7 @@
|
||||
"npmAccessLevel": "public"
|
||||
},
|
||||
"gitzone": {
|
||||
"projectType": "npm",
|
||||
"module": {
|
||||
"githost": "gitlab.com",
|
||||
"gitscope": "pushrocks",
|
||||
|
25824
package-lock.json
generated
25824
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
37
package.json
37
package.json
@ -1,13 +1,13 @@
|
||||
{
|
||||
"name": "@pushrocks/smartpromise",
|
||||
"private": false,
|
||||
"version": "3.0.6",
|
||||
"version": "3.1.7",
|
||||
"description": "simple promises and Deferred constructs",
|
||||
"main": "dist/index.js",
|
||||
"typings": "dist/index.d.ts",
|
||||
"main": "dist_ts/index.js",
|
||||
"typings": "dist_ts/index.d.ts",
|
||||
"scripts": {
|
||||
"test": "(tstest test/)",
|
||||
"build": "(tsbuild)"
|
||||
"test": "(tstest test/ --web)",
|
||||
"build": "(tsbuild --web)"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
@ -19,24 +19,27 @@
|
||||
"url": "https://gitlab.com/pushrocks/smartq/issues"
|
||||
},
|
||||
"homepage": "https://gitlab.com/pushrocks/smartq#README",
|
||||
"dependencies": {},
|
||||
"devDependencies": {
|
||||
"@gitzone/tsbuild": "^2.1.17",
|
||||
"@gitzone/tstest": "^1.0.24",
|
||||
"@pushrocks/tapbundle": "^3.0.13",
|
||||
"@types/node": "^12.7.5",
|
||||
"tslint": "^5.20.0",
|
||||
"@gitzone/tsbuild": "^2.1.25",
|
||||
"@gitzone/tstest": "^1.0.54",
|
||||
"@pushrocks/tapbundle": "^3.2.14",
|
||||
"@types/node": "^15.6.1",
|
||||
"tslint": "^6.1.3",
|
||||
"tslint-config-prettier": "^1.18.0"
|
||||
},
|
||||
"files": [
|
||||
"ts/*",
|
||||
"ts_web/*",
|
||||
"dist/*",
|
||||
"dist_web/*",
|
||||
"dist_ts_web/*",
|
||||
"assets/*",
|
||||
"ts/**/*",
|
||||
"ts_web/**/*",
|
||||
"dist/**/*",
|
||||
"dist_*/**/*",
|
||||
"dist_ts/**/*",
|
||||
"dist_ts_web/**/*",
|
||||
"assets/**/*",
|
||||
"cli.js",
|
||||
"npmextra.json",
|
||||
"readme.md"
|
||||
],
|
||||
"browserslist": [
|
||||
"last 1 chrome versions"
|
||||
]
|
||||
}
|
||||
|
98
readme.md
Normal file
98
readme.md
Normal file
@ -0,0 +1,98 @@
|
||||
# @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 | [](https://lossless.cloud)
|
||||
GitLab Pipline Test Coverage | [](https://lossless.cloud)
|
||||
npm | [](https://lossless.cloud)
|
||||
Snyk | [](https://lossless.cloud)
|
||||
TypeScript Support | [](https://lossless.cloud)
|
||||
node Support | [](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
||||
Code Style | [](https://lossless.cloud)
|
||||
PackagePhobia (total standalone install weight) | [](https://lossless.cloud)
|
||||
PackagePhobia (package size on registry) | [](https://lossless.cloud)
|
||||
BundlePhobia (total size when bundled) | [](https://lossless.cloud)
|
||||
Platform support | [](https://lossless.cloud) [](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 | **©** [Lossless GmbH](https://lossless.gmbh)
|
||||
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy)
|
||||
|
||||
[](https://maintainedby.lossless.com)
|
@ -18,12 +18,12 @@ tap.test('should let types flow through the Promise', async () => {
|
||||
|
||||
tap.test('should map callbacks', async () => {
|
||||
const inputArray = ['hi', 'awesome'];
|
||||
const myPromisified = async myInput => {
|
||||
const myPromisified = async (myInput) => {
|
||||
return myInput;
|
||||
};
|
||||
const expectPromise = expect(smartpromise.map(inputArray, myPromisified)).to.eventually.deep.equal(
|
||||
inputArray
|
||||
);
|
||||
const expectPromise = expect(
|
||||
smartpromise.map(inputArray, myPromisified)
|
||||
).to.eventually.deep.equal(inputArray);
|
||||
return expectPromise;
|
||||
});
|
||||
|
54
test/test.readme.node.ts
Normal file
54
test/test.readme.node.ts
Normal file
@ -0,0 +1,54 @@
|
||||
import * as smartpromise from '../ts';
|
||||
|
||||
// using Deferreds
|
||||
// simple deferred;
|
||||
const done = smartpromise.defer();
|
||||
done.promise.then((stringArg) => {
|
||||
console.log(stringArg);
|
||||
});
|
||||
done.resolve('hello') // whenever you are ready
|
||||
|
||||
|
||||
// using deferreds in async functions to cater callback style apis
|
||||
const myAsyncFunction = async (): Promise<string> => {
|
||||
const done = smartpromise.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";
|
||||
await done.promise;
|
||||
console.log(done.status); // logs "fullfilled"
|
||||
console.log(done.duration); // outputs the duration in millisenconds
|
||||
|
||||
return done.promise;
|
||||
};
|
||||
|
||||
let myAsyncFunction2 = async () => {
|
||||
let aString = await myAsyncFunction();
|
||||
console.log(aString); // will log 'hi' to console
|
||||
};
|
||||
|
||||
myAsyncFunction2();
|
||||
|
||||
// Resolved and Rejected promises
|
||||
// ------------------------------------------------
|
||||
smartpromise.resolvedPromise(`I'll get logged to console soon`).then((x) => {
|
||||
console.log(x);
|
||||
});
|
||||
|
||||
smartpromise
|
||||
.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);
|
||||
});
|
||||
|
||||
import {tap, expect } from '@pushrocks/tapbundle';
|
||||
|
||||
tap.test('runs through', async () => {})
|
||||
tap.start();
|
56
ts/index.ts
56
ts/index.ts
@ -42,21 +42,21 @@ export class Deferred<T> {
|
||||
}
|
||||
}
|
||||
|
||||
export let defer = <T>() => {
|
||||
export const defer = <T>() => {
|
||||
return new Deferred<T>();
|
||||
};
|
||||
|
||||
/**
|
||||
* 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);
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a new rejected promise for the provided reason.
|
||||
*/
|
||||
export let rejectedPromise = err => {
|
||||
export const rejectedPromise = (err) => {
|
||||
return Promise.reject(err);
|
||||
};
|
||||
|
||||
@ -64,16 +64,54 @@ interface IAsyncFunction<T> {
|
||||
(someArg: T): Promise<T>;
|
||||
}
|
||||
|
||||
export let map = async <T>(inputArg: T[], functionArg: IAsyncFunction<T>) => {
|
||||
let promiseArray: Promise<any>[] = [];
|
||||
let resultArray = [];
|
||||
for (let item of inputArg) {
|
||||
let promise: Promise<any> = functionArg(item);
|
||||
/**
|
||||
* 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 => {
|
||||
promise.then((x) => {
|
||||
resultArray.push(x);
|
||||
});
|
||||
}
|
||||
await Promise.all(promiseArray);
|
||||
return resultArray;
|
||||
};
|
||||
|
||||
export const timeoutWrap = async <T = any>(promiseArg: Promise<T>, timeoutInMsArg: number, rejectArg = true) => {
|
||||
return new Promise<T>((resolve, reject) => {
|
||||
setTimeout(() => {
|
||||
if (rejectArg) {
|
||||
reject(new Error('timeout'));
|
||||
} else {
|
||||
resolve(null);
|
||||
}
|
||||
}, timeoutInMsArg);
|
||||
promiseArg.then(resolve, reject);
|
||||
});
|
||||
};
|
||||
|
||||
export const timeoutAndContinue = async <T = any>(promiseArg: Promise<T>, timeoutInMsArg = 60000) => {
|
||||
return timeoutWrap(promiseArg, timeoutInMsArg, false);
|
||||
}
|
||||
|
||||
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;
|
||||
};
|
||||
|
Reference in New Issue
Block a user