Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
e39ad3b19b | |||
7220959662 | |||
8ece5891f7 | |||
d7b482184b | |||
de3f582226 |
44
README.md
44
README.md
@ -21,4 +21,48 @@ dropin replacement for q
|
|||||||
## Usage
|
## Usage
|
||||||
Use TypeScript for best in class instellisense.
|
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)
|
||||||
|
})
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
[](https://push.rocks)
|
[](https://push.rocks)
|
||||||
|
60
dist/index.d.ts
vendored
Normal file
60
dist/index.d.ts
vendored
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
import 'typings-global';
|
||||||
|
export interface IResolve<T> {
|
||||||
|
(value?: T | Promise<T>): void;
|
||||||
|
}
|
||||||
|
export interface IReject {
|
||||||
|
(reason?: any): void;
|
||||||
|
}
|
||||||
|
export declare class Deferred<T> {
|
||||||
|
promise: Promise<T>;
|
||||||
|
resolve: IResolve<T>;
|
||||||
|
reject: IReject;
|
||||||
|
constructor();
|
||||||
|
}
|
||||||
|
export declare let defer: <T>() => 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 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>;
|
||||||
|
};
|
31
dist/index.js
vendored
Normal file
31
dist/index.js
vendored
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
"use strict";
|
||||||
|
require("typings-global");
|
||||||
|
class Deferred {
|
||||||
|
constructor() {
|
||||||
|
this.promise = new Promise((resolve, reject) => {
|
||||||
|
this.resolve = resolve;
|
||||||
|
this.reject = reject;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
exports.Deferred = Deferred;
|
||||||
|
exports.defer = () => {
|
||||||
|
return new Deferred();
|
||||||
|
};
|
||||||
|
/**
|
||||||
|
* 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=
|
12
package.json
12
package.json
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "smartq",
|
"name": "smartq",
|
||||||
"version": "1.0.1",
|
"version": "1.0.3",
|
||||||
"description": "dropin replacement for q",
|
"description": "dropin replacement for q",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"typings": "dist/index.d.ts",
|
"typings": "dist/index.d.ts",
|
||||||
@ -16,5 +16,13 @@
|
|||||||
"bugs": {
|
"bugs": {
|
||||||
"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": {
|
||||||
|
"typings-global": "^1.0.14"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@types/should": "^8.1.30",
|
||||||
|
"should": "^11.1.2",
|
||||||
|
"typings-test": "^1.0.3"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
1
test/test.d.ts
vendored
Normal file
1
test/test.d.ts
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
import 'typings-test';
|
23
test/test.js
Normal file
23
test/test.js
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
"use strict";
|
||||||
|
require("typings-test");
|
||||||
|
const should = require("should");
|
||||||
|
const q = require("../dist/index");
|
||||||
|
describe('smartq', function () {
|
||||||
|
it('should return a Deferred for .defer()', function (done) {
|
||||||
|
let myDeferred = q.defer();
|
||||||
|
myDeferred.promise.then(() => {
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
myDeferred.resolve();
|
||||||
|
});
|
||||||
|
it('should let types flow through the Promise', function (done) {
|
||||||
|
let myString = 'someString';
|
||||||
|
let myDeferred = q.defer();
|
||||||
|
myDeferred.promise.then(x => {
|
||||||
|
should(x).equal('someString');
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
myDeferred.resolve(myString);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGVzdC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbInRlc3QudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLHdCQUFxQjtBQUVyQixpQ0FBZ0M7QUFDaEMsbUNBQWtDO0FBRWxDLFFBQVEsQ0FBQyxRQUFRLEVBQUU7SUFDZixFQUFFLENBQUMsdUNBQXVDLEVBQUUsVUFBUyxJQUFJO1FBQ3JELElBQUksVUFBVSxHQUFHLENBQUMsQ0FBQyxLQUFLLEVBQUUsQ0FBQTtRQUMxQixVQUFVLENBQUMsT0FBTyxDQUFDLElBQUksQ0FBQztZQUNwQixJQUFJLEVBQUUsQ0FBQTtRQUNWLENBQUMsQ0FBQyxDQUFBO1FBQ0YsVUFBVSxDQUFDLE9BQU8sRUFBRSxDQUFBO0lBQ3hCLENBQUMsQ0FBQyxDQUFBO0lBRUYsRUFBRSxDQUFDLDJDQUEyQyxFQUFFLFVBQVMsSUFBSTtRQUN6RCxJQUFJLFFBQVEsR0FBRyxZQUFZLENBQUE7UUFDM0IsSUFBSSxVQUFVLEdBQUcsQ0FBQyxDQUFDLEtBQUssRUFBVSxDQUFBO1FBQ2xDLFVBQVUsQ0FBQyxPQUFPLENBQUMsSUFBSSxDQUFDLENBQUM7WUFDckIsTUFBTSxDQUFDLENBQUMsQ0FBQyxDQUFDLEtBQUssQ0FBQyxZQUFZLENBQUMsQ0FBQTtZQUM3QixJQUFJLEVBQUUsQ0FBQTtRQUNWLENBQUMsQ0FBQyxDQUFBO1FBQ0YsVUFBVSxDQUFDLE9BQU8sQ0FBQyxRQUFRLENBQUMsQ0FBQTtJQUNoQyxDQUFDLENBQUMsQ0FBQTtBQUNOLENBQUMsQ0FBQyxDQUFBIn0=
|
24
test/test.ts
Normal file
24
test/test.ts
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
import 'typings-test'
|
||||||
|
|
||||||
|
import * as should from 'should'
|
||||||
|
import * as q from '../dist/index'
|
||||||
|
|
||||||
|
describe('smartq', function() {
|
||||||
|
it('should return a Deferred for .defer()', function(done) {
|
||||||
|
let myDeferred = q.defer()
|
||||||
|
myDeferred.promise.then(() => {
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
myDeferred.resolve()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should let types flow through the Promise', function(done) {
|
||||||
|
let myString = 'someString'
|
||||||
|
let myDeferred = q.defer<string>()
|
||||||
|
myDeferred.promise.then(x => {
|
||||||
|
should(x).equal('someString')
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
myDeferred.resolve(myString)
|
||||||
|
})
|
||||||
|
})
|
45
ts/index.ts
Normal file
45
ts/index.ts
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
import 'typings-global'
|
||||||
|
|
||||||
|
export interface IResolve<T> {
|
||||||
|
(value?: T | Promise<T>): void
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface IReject {
|
||||||
|
(reason?: any): void
|
||||||
|
}
|
||||||
|
|
||||||
|
export class Deferred<T> {
|
||||||
|
promise: Promise<T>
|
||||||
|
resolve: IResolve<T>
|
||||||
|
reject: IReject
|
||||||
|
constructor() {
|
||||||
|
this.promise = new Promise<T>((resolve, reject) => {
|
||||||
|
this.resolve = resolve
|
||||||
|
this.reject = reject
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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
|
Reference in New Issue
Block a user