Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
1848006601 | |||
c761cc4460 | |||
9f6d88e162 | |||
255d6415a5 | |||
84a8d8431c | |||
95d27d52a7 | |||
1363aa7957 | |||
095ea36e5d |
17570
package-lock.json
generated
17570
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartpromise",
|
"name": "@pushrocks/smartpromise",
|
||||||
"private": false,
|
"private": false,
|
||||||
"version": "3.1.2",
|
"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"
|
||||||
},
|
},
|
||||||
|
94
readme.md
94
readme.md
@ -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
|
||||||
|
|
||||||
[](https://gitlab.com/pushrocks/smartpromise/commits/master)
|
Status Category | Status Badge
|
||||||
[](https://gitlab.com/pushrocks/smartpromise/commits/master)
|
-- | --
|
||||||
[](https://www.npmjs.com/package/@pushrocks/smartpromise)
|
GitLab Pipelines | [](https://lossless.cloud)
|
||||||
[](https://snyk.io/test/npm/@pushrocks/smartpromise)
|
GitLab Pipline Test Coverage | [](https://lossless.cloud)
|
||||||
[](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
npm | [](https://lossless.cloud)
|
||||||
[](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
Snyk | [](https://lossless.cloud)
|
||||||
[](https://prettier.io/)
|
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
|
## 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 | **©** [Lossless GmbH](https://lossless.gmbh)
|
> MIT licensed | **©** [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)
|
||||||
|
|
||||||
[](https://maintainedby.lossless.com)
|
[](https://maintainedby.lossless.com)
|
||||||
|
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();
|
17
ts/index.ts
17
ts/index.ts
@ -85,10 +85,25 @@ export const map = async <T>(inputArg: T[], functionArg: IAsyncFunction<T>) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const timeoutWrap = <T = any>(promiseArg: Promise<T>, timeoutInMs: number) => {
|
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'));
|
||||||
}, timeoutInMs);
|
}, timeoutInMs);
|
||||||
promiseArg.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;
|
||||||
|
};
|
||||||
|
Reference in New Issue
Block a user