Compare commits

...

10 Commits

Author SHA1 Message Date
1848006601 3.1.6 2021-05-31 13:02:23 +02:00
c761cc4460 fix(core): update 2021-05-31 13:02:22 +02:00
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
6 changed files with 16233 additions and 1519 deletions

17570
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,7 +1,7 @@
{ {
"name": "@pushrocks/smartpromise", "name": "@pushrocks/smartpromise",
"private": false, "private": false,
"version": "3.1.1", "version": "3.1.6",
"description": "simple promises and Deferred constructs", "description": "simple promises and Deferred constructs",
"main": "dist_ts/index.js", "main": "dist_ts/index.js",
"typings": "dist_ts/index.d.ts", "typings": "dist_ts/index.d.ts",
@ -19,12 +19,11 @@
"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": {},
"devDependencies": { "devDependencies": {
"@gitzone/tsbuild": "^2.1.25", "@gitzone/tsbuild": "^2.1.25",
"@gitzone/tstest": "^1.0.52", "@gitzone/tstest": "^1.0.54",
"@pushrocks/tapbundle": "^3.2.9", "@pushrocks/tapbundle": "^3.2.14",
"@types/node": "^14.11.8", "@types/node": "^15.6.1",
"tslint": "^6.1.3", "tslint": "^6.1.3",
"tslint-config-prettier": "^1.18.0" "tslint-config-prettier": "^1.18.0"
}, },

View File

@ -1,23 +1,27 @@
# @pushrocks/smartpromise # @pushrocks/smartpromise
simple promises and Deferred constructs simple promises and Deferred constructs
## Availabililty and Links ## Availabililty and Links
* [npmjs.org (npm package)](https://www.npmjs.com/package/@pushrocks/smartpromise)
- [npmjs.org (npm package)](https://www.npmjs.com/package/@pushrocks/smartpromise) * [gitlab.com (source)](https://gitlab.com/pushrocks/smartpromise)
- [gitlab.com (source)](https://gitlab.com/pushrocks/smartpromise) * [github.com (source mirror)](https://github.com/pushrocks/smartpromise)
- [github.com (source mirror)](https://github.com/pushrocks/smartpromise) * [docs (typedoc)](https://pushrocks.gitlab.io/smartpromise/)
- [docs (typedoc)](https://pushrocks.gitlab.io/smartpromise/)
## Status for master ## Status for master
[![build status](https://gitlab.com/pushrocks/smartpromise/badges/master/build.svg)](https://gitlab.com/pushrocks/smartpromise/commits/master) Status Category | Status Badge
[![coverage report](https://gitlab.com/pushrocks/smartpromise/badges/master/coverage.svg)](https://gitlab.com/pushrocks/smartpromise/commits/master) -- | --
[![npm downloads per month](https://img.shields.io/npm/dm/@pushrocks/smartpromise.svg)](https://www.npmjs.com/package/@pushrocks/smartpromise) GitLab Pipelines | [![pipeline status](https://gitlab.com/pushrocks/smartpromise/badges/master/pipeline.svg)](https://lossless.cloud)
[![Known Vulnerabilities](https://snyk.io/test/npm/@pushrocks/smartpromise/badge.svg)](https://snyk.io/test/npm/@pushrocks/smartpromise) GitLab Pipline Test Coverage | [![coverage report](https://gitlab.com/pushrocks/smartpromise/badges/master/coverage.svg)](https://lossless.cloud)
[![TypeScript](https://img.shields.io/badge/TypeScript->=%203.x-blue.svg)](https://nodejs.org/dist/latest-v10.x/docs/api/) npm | [![npm downloads per month](https://badgen.net/npm/dy/@pushrocks/smartpromise)](https://lossless.cloud)
[![node](https://img.shields.io/badge/node->=%2010.x.x-blue.svg)](https://nodejs.org/dist/latest-v10.x/docs/api/) Snyk | [![Known Vulnerabilities](https://badgen.net/snyk/pushrocks/smartpromise)](https://lossless.cloud)
[![JavaScript Style Guide](https://img.shields.io/badge/code%20style-prettier-ff69b4.svg)](https://prettier.io/) 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 ## Usage
@ -27,64 +31,68 @@ Use TypeScript for best in class instellisense.
> smartq does not repeat any native functions, so for things like .all() simply use Promise.all() > smartq does not repeat any native functions, so for things like .all() simply use Promise.all()
```typescript ```typescript
import * as q from '@pushrocks/smartpromise' import * as q from '@pushrocks/smartpromise';
// Deferred // Deferred
// ----------------------------------------------- // -----------------------------------------------
let myAsyncFunction = (): Promise<string> => { let myAsyncFunction = (): Promise<string> => {
let done = q.defer<string>() // returns your typical Deferred object let done = q.defer<string>(); // returns your typical Deferred object
setTimeout(() => { setTimeout(() => {
done.resolve('hi') // will throw type error for other types than string as argument ;) done.resolve('hi'); // will throw type error for other types than string as argument ;)
}, 6000); }, 6000);
console.log(done.status) // logs "pending"; console.log(done.status); // logs "pending";
done.promise.then(() => { done.promise.then(() => {
console.log(done.status) // logs "fullfilled" console.log(done.status); // logs "fullfilled"
console.log(done.duration) // logs the milliseconds between instantiation and fullfillment console.log(done.duration); // logs the milliseconds between instantiation and fullfillment
}) });
return done.promise return done.promise;
} };
let myAsyncFunction2 = async () => { let myAsyncFunction2 = async () => {
let aString = await myAsyncFunction() let aString = await myAsyncFunction();
console.log(aString) // will log 'hi' to console console.log(aString); // will log 'hi' to console
} };
myAsyncFunction2(); myAsyncFunction2();
// Resolved and Rejected promises // Resolved and Rejected promises
// ------------------------------------------------ // ------------------------------------------------
q.resolvedPromise(`I'll get logged to console soon`) q.resolvedPromise(`I'll get logged to console soon`).then((x) => {
.then(x => { console.log(x);
console.log(x) });
})
q.rejectedPromise(`what a lovely error message`) q.rejectedPromise(`what a lovely error message`)
.then(() => { .then(
console.log('This never makes it to console') () => {
}/*, alternatively put a reject function here */) console.log('This never makes it to console');
.catch(err => { } /*, alternatively put a reject function here */
console.log(err) )
}) .catch((err) => {
console.log(err);
});
// Promisify (typed) // Promisify (typed)
// ------------------------------------------------ // ------------------------------------------------
let myCallbackedFunction = (someString: string, someNumber: number, cb) => { let myCallbackedFunction = (someString: string, someNumber: number, cb) => {
cb(null, someString) cb(null, someString);
} };
let myPromisedFunction = q.promisify(myCallbackFunction) let myPromisedFunction = q.promisify(myCallbackFunction);
myPromisedFunction('helloThere', 2).then(x => { myPromisedFunction('helloThere', 2).then((x) => {
console.log(x) // will log 'helloThere' to console 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. For further information read the linked docs at the top of this readme.
> MIT licensed | **&copy;** [Lossless GmbH](https://lossless.gmbh) > MIT licensed | **&copy;** [Lossless GmbH](https://lossless.gmbh)
> | By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy) | 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) [![repo-footer](https://lossless.gitlab.io/publicrelations/repofooter.svg)](https://maintainedby.lossless.com)

54
test/test.readme.node.ts Normal file
View 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();

View File

@ -84,11 +84,26 @@ export const map = async <T>(inputArg: T[], functionArg: IAsyncFunction<T>) => {
return resultArray; return resultArray;
}; };
export const timeoutWrap = (ms, promise) => { export const timeoutWrap = <T = any>(promiseArg: Promise<T>, timeoutInMs: number) => {
return new Promise((resolve, reject) => { return new Promise<T>((resolve, reject) => {
setTimeout(() => { setTimeout(() => {
reject(new Error('timeout')); reject(new Error('timeout'));
}, ms); }, timeoutInMs);
promise.then(resolve, reject); 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;
};