ad better readme and some more functions

This commit is contained in:
Philipp Kunz 2017-01-20 21:58:53 +01:00
parent 8ece5891f7
commit 7220959662
4 changed files with 124 additions and 6 deletions

View File

@ -21,4 +21,48 @@ dropin replacement for q
## Usage
Use TypeScript for best in class instellisense.
> Note: smartq uses native ES6 promises
```javascript
import * as q from 'smartq'
let myAsyncFunction = (): Promise<string> => {
let done = q.defer() // returns your typical Deferred object
setTimeout(() => {
done.resolve('hi')
},6000)
return done.promise
}
let myAsyncFunction2 = async () => {
let aString = await myAsyncFunction()
console.log(aString) // will log 'hi' to console
}
myAsyncFunction2();
q.all(myAsyncFunction(), myAsyncFunction2())
.then(() => {
console.log('all promises for q.all have been fullfilled')
})
q.race(/* some promises here */)
.then(() => {
console.log('at least one promise for q.race is fullfilled')
})
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)
})
```
[![npm](https://push.rocks/assets/repo-header.svg)](https://push.rocks)

47
dist/index.d.ts vendored
View File

@ -12,4 +12,49 @@ export declare class Deferred<T> {
constructor();
}
export declare let defer: <T>() => Deferred<T>;
export declare let all: () => void;
/**
* Creates a Promise that is resolved with an array of results when all of the provided Promises resolve, or rejected when any Promise is rejected.
*/
export declare let all: {
<TAll>(values: Iterable<TAll | PromiseLike<TAll>>): Promise<TAll[]>;
<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike<T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>, T7 | PromiseLike<T7>, T8 | PromiseLike<T8>, T9 | PromiseLike<T9>, T10 | PromiseLike<T10>]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10]>;
<T1, T2, T3, T4, T5, T6, T7, T8, T9>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike<T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>, T7 | PromiseLike<T7>, T8 | PromiseLike<T8>, T9 | PromiseLike<T9>]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8, T9]>;
<T1, T2, T3, T4, T5, T6, T7, T8>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike<T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>, T7 | PromiseLike<T7>, T8 | PromiseLike<T8>]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8]>;
<T1, T2, T3, T4, T5, T6, T7>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike<T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>, T7 | PromiseLike<T7>]): Promise<[T1, T2, T3, T4, T5, T6, T7]>;
<T1, T2, T3, T4, T5, T6>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike<T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>]): Promise<[T1, T2, T3, T4, T5, T6]>;
<T1, T2, T3, T4, T5>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike<T4>, T5 | PromiseLike<T5>]): Promise<[T1, T2, T3, T4, T5]>;
<T1, T2, T3, T4>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike<T4>]): Promise<[T1, T2, T3, T4]>;
<T1, T2, T3>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>]): Promise<[T1, T2, T3]>;
<T1, T2>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>]): Promise<[T1, T2]>;
<T>(values: (T | PromiseLike<T>)[]): Promise<T[]>;
};
/**
* Creates a Promise that is resolved or rejected when any of the provided Promises are resolved or rejected.
*/
export declare let race: {
<T>(values: Iterable<T | PromiseLike<T>>): Promise<T>;
<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike<T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>, T7 | PromiseLike<T7>, T8 | PromiseLike<T8>, T9 | PromiseLike<T9>, T10 | PromiseLike<T10>]): Promise<T1 | T2 | T3 | T4 | T5 | T6 | T7 | T8 | T9 | T10>;
<T1, T2, T3, T4, T5, T6, T7, T8, T9>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike<T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>, T7 | PromiseLike<T7>, T8 | PromiseLike<T8>, T9 | PromiseLike<T9>]): Promise<T1 | T2 | T3 | T4 | T5 | T6 | T7 | T8 | T9>;
<T1, T2, T3, T4, T5, T6, T7, T8>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike<T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>, T7 | PromiseLike<T7>, T8 | PromiseLike<T8>]): Promise<T1 | T2 | T3 | T4 | T5 | T6 | T7 | T8>;
<T1, T2, T3, T4, T5, T6, T7>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike<T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>, T7 | PromiseLike<T7>]): Promise<T1 | T2 | T3 | T4 | T5 | T6 | T7>;
<T1, T2, T3, T4, T5, T6>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike<T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>]): Promise<T1 | T2 | T3 | T4 | T5 | T6>;
<T1, T2, T3, T4, T5>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike<T4>, T5 | PromiseLike<T5>]): Promise<T1 | T2 | T3 | T4 | T5>;
<T1, T2, T3, T4>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike<T4>]): Promise<T1 | T2 | T3 | T4>;
<T1, T2, T3>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>]): Promise<T1 | T2 | T3>;
<T1, T2>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>]): Promise<T1 | T2>;
<T>(values: (T | PromiseLike<T>)[]): Promise<T>;
};
/**
* Creates a new resolved promise for the provided value.
*/
export declare let resolvedPromise: {
<T>(value: T | PromiseLike<T>): Promise<T>;
(): Promise<void>;
};
/**
* Creates a new rejected promise for the provided reason.
*/
export declare let rejectedPromise: {
(reason: any): Promise<never>;
<T>(reason: any): Promise<T>;
};

20
dist/index.js vendored
View File

@ -12,6 +12,20 @@ exports.Deferred = Deferred;
exports.defer = () => {
return new Deferred();
};
exports.all = () => {
};
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUEsMEJBQXVCO0FBVXZCO0lBSUk7UUFDSSxJQUFJLENBQUMsT0FBTyxHQUFHLElBQUksT0FBTyxDQUFJLENBQUMsT0FBTyxFQUFFLE1BQU07WUFDMUMsSUFBSSxDQUFDLE9BQU8sR0FBRyxPQUFPLENBQUE7WUFDdEIsSUFBSSxDQUFDLE1BQU0sR0FBRyxNQUFNLENBQUE7UUFDeEIsQ0FBQyxDQUFDLENBQUE7SUFDTixDQUFDO0NBQ0o7QUFWRCw0QkFVQztBQUVVLFFBQUEsS0FBSyxHQUFHO0lBQ2YsTUFBTSxDQUFDLElBQUksUUFBUSxFQUFLLENBQUE7QUFDNUIsQ0FBQyxDQUFBO0FBR1UsUUFBQSxHQUFHLEdBQUc7QUFFakIsQ0FBQyxDQUFBIn0=
/**
* Creates a Promise that is resolved with an array of results when all of the provided Promises resolve, or rejected when any Promise is rejected.
*/
exports.all = Promise.all;
/**
* Creates a Promise that is resolved or rejected when any of the provided Promises are resolved or rejected.
*/
exports.race = Promise.race;
/**
* Creates a new resolved promise for the provided value.
*/
exports.resolvedPromise = Promise.resolve;
/**
* Creates a new rejected promise for the provided reason.
*/
exports.rejectedPromise = Promise.reject;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUEsMEJBQXVCO0FBVXZCO0lBSUk7UUFDSSxJQUFJLENBQUMsT0FBTyxHQUFHLElBQUksT0FBTyxDQUFJLENBQUMsT0FBTyxFQUFFLE1BQU07WUFDMUMsSUFBSSxDQUFDLE9BQU8sR0FBRyxPQUFPLENBQUE7WUFDdEIsSUFBSSxDQUFDLE1BQU0sR0FBRyxNQUFNLENBQUE7UUFDeEIsQ0FBQyxDQUFDLENBQUE7SUFDTixDQUFDO0NBQ0o7QUFWRCw0QkFVQztBQUVVLFFBQUEsS0FBSyxHQUFHO0lBQ2YsTUFBTSxDQUFDLElBQUksUUFBUSxFQUFLLENBQUE7QUFDNUIsQ0FBQyxDQUFBO0FBRUQ7O0dBRUc7QUFDUSxRQUFBLEdBQUcsR0FBRyxPQUFPLENBQUMsR0FBRyxDQUFBO0FBRTVCOztHQUVHO0FBQ1EsUUFBQSxJQUFJLEdBQUcsT0FBTyxDQUFDLElBQUksQ0FBQTtBQUU5Qjs7R0FFRztBQUNRLFFBQUEsZUFBZSxHQUFHLE9BQU8sQ0FBQyxPQUFPLENBQUE7QUFFNUM7O0dBRUc7QUFDUSxRQUFBLGVBQWUsR0FBRyxPQUFPLENBQUMsTUFBTSxDQUFBIn0=

View File

@ -24,7 +24,22 @@ export let defer = <T>() => {
return new Deferred<T>()
}
/**
* Creates a Promise that is resolved with an array of results when all of the provided Promises resolve, or rejected when any Promise is rejected.
*/
export let all = Promise.all
export let all = () => {
/**
* Creates a Promise that is resolved or rejected when any of the provided Promises are resolved or rejected.
*/
export let race = Promise.race
}
/**
* Creates a new resolved promise for the provided value.
*/
export let resolvedPromise = Promise.resolve
/**
* Creates a new rejected promise for the provided reason.
*/
export let rejectedPromise = Promise.reject