Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
6a4ee22f36 | |||
e23f946e50 | |||
8d62fc6ef1 | |||
e39ad3b19b | |||
7220959662 | |||
8ece5891f7 | |||
d7b482184b | |||
de3f582226 |
36
README.md
36
README.md
@ -21,4 +21,40 @@ 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
|
||||||
|
> smartq does not repeat any native functions, so for things like .all() simply use Promise.all()
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
import * as q from 'smartq'
|
||||||
|
|
||||||
|
let myAsyncFunction = (): Promise<string> => {
|
||||||
|
let done = q.defer<string>() // returns your typical Deferred object
|
||||||
|
setTimeout(() => {
|
||||||
|
done.resolve('hi') // will throw type error for other types than string as argument ;)
|
||||||
|
},6000)
|
||||||
|
return done.promise
|
||||||
|
}
|
||||||
|
|
||||||
|
let myAsyncFunction2 = async () => {
|
||||||
|
let aString = await myAsyncFunction()
|
||||||
|
console.log(aString) // will log 'hi' to console
|
||||||
|
}
|
||||||
|
|
||||||
|
myAsyncFunction2();
|
||||||
|
|
||||||
|
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)
|
||||||
|
22
dist/index.d.ts
vendored
Normal file
22
dist/index.d.ts
vendored
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
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 new resolved promise for the provided value.
|
||||||
|
*/
|
||||||
|
export declare let resolvedPromise: <T>(value?: T) => Promise<T>;
|
||||||
|
/**
|
||||||
|
* Creates a new rejected promise for the provided reason.
|
||||||
|
*/
|
||||||
|
export declare let rejectedPromise: (err: any) => Promise<never>;
|
27
dist/index.js
vendored
Normal file
27
dist/index.js
vendored
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
"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 new resolved promise for the provided value.
|
||||||
|
*/
|
||||||
|
exports.resolvedPromise = (value) => {
|
||||||
|
return Promise.resolve(value);
|
||||||
|
};
|
||||||
|
/**
|
||||||
|
* Creates a new rejected promise for the provided reason.
|
||||||
|
*/
|
||||||
|
exports.rejectedPromise = (err) => {
|
||||||
|
return Promise.reject(err);
|
||||||
|
};
|
||||||
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUEsMEJBQXVCO0FBVXZCO0lBSUk7UUFDSSxJQUFJLENBQUMsT0FBTyxHQUFHLElBQUksT0FBTyxDQUFJLENBQUMsT0FBTyxFQUFFLE1BQU07WUFDMUMsSUFBSSxDQUFDLE9BQU8sR0FBRyxPQUFPLENBQUE7WUFDdEIsSUFBSSxDQUFDLE1BQU0sR0FBRyxNQUFNLENBQUE7UUFDeEIsQ0FBQyxDQUFDLENBQUE7SUFDTixDQUFDO0NBQ0o7QUFWRCw0QkFVQztBQUVVLFFBQUEsS0FBSyxHQUFHO0lBQ2YsTUFBTSxDQUFDLElBQUksUUFBUSxFQUFLLENBQUE7QUFDNUIsQ0FBQyxDQUFBO0FBRUQ7O0dBRUc7QUFDUSxRQUFBLGVBQWUsR0FBRyxDQUFJLEtBQVM7SUFDdEMsTUFBTSxDQUFDLE9BQU8sQ0FBQyxPQUFPLENBQUMsS0FBSyxDQUFDLENBQUE7QUFDakMsQ0FBQyxDQUFBO0FBRUQ7O0dBRUc7QUFDUSxRQUFBLGVBQWUsR0FBRyxDQUFDLEdBQUc7SUFDN0IsTUFBTSxDQUFDLE9BQU8sQ0FBQyxNQUFNLENBQUMsR0FBRyxDQUFDLENBQUE7QUFDOUIsQ0FBQyxDQUFBIn0=
|
12
package.json
12
package.json
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "smartq",
|
"name": "smartq",
|
||||||
"version": "1.0.1",
|
"version": "1.0.4",
|
||||||
"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)
|
||||||
|
})
|
||||||
|
})
|
39
ts/index.ts
Normal file
39
ts/index.ts
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
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 new resolved promise for the provided value.
|
||||||
|
*/
|
||||||
|
export let resolvedPromise = <T>(value?: T): Promise<T> => {
|
||||||
|
return Promise.resolve(value)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new rejected promise for the provided reason.
|
||||||
|
*/
|
||||||
|
export let rejectedPromise = (err) => {
|
||||||
|
return Promise.reject(err)
|
||||||
|
}
|
Reference in New Issue
Block a user