smartpromise/readme.md

97 lines
4.1 KiB
Markdown
Raw Normal View History

# @pushrocks/smartpromise
simple promises and Deferred constructs
## Availabililty and Links
2021-04-23 17:58:37 +00:00
* [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
2021-04-23 17:58:37 +00:00
Status Category | Status Badge
-- | --
GitLab Pipelines | [![pipeline status](https://gitlab.com/pushrocks/smartpromise/badges/master/pipeline.svg)](https://lossless.cloud)
GitLab Pipline Test Coverage | [![coverage report](https://gitlab.com/pushrocks/smartpromise/badges/master/coverage.svg)](https://lossless.cloud)
npm | [![npm downloads per month](https://badgen.net/npm/dy/@pushrocks/smartpromise)](https://lossless.cloud)
Snyk | [![Known Vulnerabilities](https://badgen.net/snyk/pushrocks/smartpromise)](https://lossless.cloud)
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)
## 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
2021-04-23 17:58:37 +00:00
import * as q from '@pushrocks/smartpromise';
// Deferred
// -----------------------------------------------
let myAsyncFunction = (): Promise<string> => {
2021-04-23 17:58:37 +00:00
let done = q.defer<string>(); // returns your typical Deferred object
setTimeout(() => {
2021-04-23 17:58:37 +00:00
done.resolve('hi'); // will throw type error for other types than string as argument ;)
}, 6000);
2021-04-23 17:58:37 +00:00
console.log(done.status); // logs "pending";
done.promise.then(() => {
2021-04-23 17:58:37 +00:00
console.log(done.status); // logs "fullfilled"
console.log(done.duration); // logs the milliseconds between instantiation and fullfillment
});
2021-04-23 17:58:37 +00:00
return done.promise;
};
let myAsyncFunction2 = async () => {
2021-04-23 17:58:37 +00:00
let aString = await myAsyncFunction();
console.log(aString); // will log 'hi' to console
};
myAsyncFunction2();
// Resolved and Rejected promises
// ------------------------------------------------
2021-04-23 17:58:37 +00:00
q.resolvedPromise(`I'll get logged to console soon`).then((x) => {
console.log(x);
});
q.rejectedPromise(`what a lovely error message`)
2021-04-23 17:58:37 +00:00
.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) => {
2021-04-23 17:58:37 +00:00
cb(null, someString);
};
2021-04-23 17:58:37 +00:00
let myPromisedFunction = q.promisify(myCallbackFunction);
myPromisedFunction('helloThere', 2).then((x) => {
console.log(x); // will log 'helloThere' to console
});
```
2021-04-23 17:58:37 +00:00
## 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.
2023-04-04 18:22:57 +00:00
## Legal
> MIT licensed | **&copy;** [Task Venture Capital GmbH](https://task.vc)
2021-04-23 17:58:37 +00:00
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy)