add promisify
This commit is contained in:
parent
6a4ee22f36
commit
1eea124cf4
22
README.md
22
README.md
@ -27,6 +27,8 @@ Use TypeScript for best in class instellisense.
|
|||||||
```javascript
|
```javascript
|
||||||
import * as q from 'smartq'
|
import * as q from 'smartq'
|
||||||
|
|
||||||
|
// 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(() => {
|
||||||
@ -42,6 +44,9 @@ let myAsyncFunction2 = async () => {
|
|||||||
|
|
||||||
myAsyncFunction2();
|
myAsyncFunction2();
|
||||||
|
|
||||||
|
|
||||||
|
// 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)
|
||||||
@ -55,6 +60,23 @@ q.rejectedPromise(`what a lovely error message`)
|
|||||||
console.log(err)
|
console.log(err)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Promisify (typed)
|
||||||
|
// ------------------------------------------------
|
||||||
|
|
||||||
|
let myCallbackedFunction = (someString: string, someNumber: number, cb) => {
|
||||||
|
cb(null, someString)
|
||||||
|
}
|
||||||
|
|
||||||
|
let myPromisedFunction = q.promisify(myCallbackFunction)
|
||||||
|
myPromisedFunction('helloThere', 2).then(x => {
|
||||||
|
console.log(x) // will log 'helloThere' to console
|
||||||
|
})
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Dependency Credits:
|
||||||
|
who | made what
|
||||||
|
-- | --
|
||||||
|
[notenoughneon](https://www.npmjs.com/~notenoughneon) | [typed-promisify](https://www.npmjs.com/package/typed-promisify)
|
||||||
|
|
||||||
[![npm](https://push.rocks/assets/repo-header.svg)](https://push.rocks)
|
[![npm](https://push.rocks/assets/repo-header.svg)](https://push.rocks)
|
||||||
|
3
dist/index.d.ts
vendored
3
dist/index.d.ts
vendored
@ -1,4 +1,5 @@
|
|||||||
import 'typings-global';
|
import 'typings-global';
|
||||||
|
import * as typedPromisify from 'typed-promisify';
|
||||||
export interface IResolve<T> {
|
export interface IResolve<T> {
|
||||||
(value?: T | Promise<T>): void;
|
(value?: T | Promise<T>): void;
|
||||||
}
|
}
|
||||||
@ -20,3 +21,5 @@ export declare let resolvedPromise: <T>(value?: T) => Promise<T>;
|
|||||||
* Creates a new rejected promise for the provided reason.
|
* Creates a new rejected promise for the provided reason.
|
||||||
*/
|
*/
|
||||||
export declare let rejectedPromise: (err: any) => Promise<never>;
|
export declare let rejectedPromise: (err: any) => Promise<never>;
|
||||||
|
export declare let promisify: typeof typedPromisify.promisify;
|
||||||
|
export declare let map: typeof typedPromisify.map;
|
||||||
|
5
dist/index.js
vendored
5
dist/index.js
vendored
@ -1,5 +1,6 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
require("typings-global");
|
require("typings-global");
|
||||||
|
const typedPromisify = require("typed-promisify");
|
||||||
class Deferred {
|
class Deferred {
|
||||||
constructor() {
|
constructor() {
|
||||||
this.promise = new Promise((resolve, reject) => {
|
this.promise = new Promise((resolve, reject) => {
|
||||||
@ -24,4 +25,6 @@ exports.resolvedPromise = (value) => {
|
|||||||
exports.rejectedPromise = (err) => {
|
exports.rejectedPromise = (err) => {
|
||||||
return Promise.reject(err);
|
return Promise.reject(err);
|
||||||
};
|
};
|
||||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUEsMEJBQXVCO0FBVXZCO0lBSUk7UUFDSSxJQUFJLENBQUMsT0FBTyxHQUFHLElBQUksT0FBTyxDQU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04sQ0FBQztDQUNKO0FBVkQsNEJBVUM7QUFFVSxRQUFBLEtBQUssR0FBRztJQUNmLE1BQU0sQ0FBQyxJQUFJLFFBQVEsRUFBSyxDQUFBO0FBQzVCLENBQUMsQ0FBQTtBQUVEOztHQUVHO0FBQ1EsUUFBQSxlQUFlLEdBQUcsQ0FBSSxLQUFTO0lBQ3RDLE1BQU0sQ0FBQyxPQUFPLENBQUMsT0FBTyxDQUFDLEtBQUssQ0FBQyxDQUFBO0FBQ2pDLENBQUMsQ0FBQTtBQUVEOztHQUVHO0FBQ1EsUUFBQSxlQUFlLEdBQUcsQ0FBQyxHQUFHO0lBQzdCLE1BQU0sQ0FBQyxPQUFPLENBQUMsTUFBTSxDQUFDLEdBQUcsQ0FBQyxDQUFBO0FBQzlCLENBQUMsQ0FBQTtBQUVVLFFBQUEsU0FBUyxHQUFHLGNBQWMsQ0FBQyxTQUFTLENBQUE7QUFDcEMsUUFBQSxHQUFHLEdBQUcsY0FBYyxDQUFDLEdBQUcsQ0FBQSJ9
|
@ -18,11 +18,11 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://gitlab.com/pushrocks/smartq#README",
|
"homepage": "https://gitlab.com/pushrocks/smartq#README",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"typed-promisify": "^0.3.0",
|
||||||
"typings-global": "^1.0.14"
|
"typings-global": "^1.0.14"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/should": "^8.1.30",
|
"smartchai": "^1.0.1",
|
||||||
"should": "^11.1.2",
|
|
||||||
"typings-test": "^1.0.3"
|
"typings-test": "^1.0.3"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
33
test/test.js
33
test/test.js
@ -1,23 +1,34 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
require("typings-test");
|
require("typings-test");
|
||||||
const should = require("should");
|
const smartchai_1 = require("smartchai");
|
||||||
const q = require("../dist/index");
|
const q = require("../dist/index");
|
||||||
|
let myCallback = (someValue1, cb) => {
|
||||||
|
cb(null, someValue1);
|
||||||
|
};
|
||||||
describe('smartq', function () {
|
describe('smartq', function () {
|
||||||
it('should return a Deferred for .defer()', function (done) {
|
it('should return a Deferred for .defer()', function () {
|
||||||
let myDeferred = q.defer();
|
let myDeferred = q.defer();
|
||||||
myDeferred.promise.then(() => {
|
let expectPromise = smartchai_1.expect(myDeferred.promise).to.eventually.be.fulfilled;
|
||||||
done();
|
|
||||||
});
|
|
||||||
myDeferred.resolve();
|
myDeferred.resolve();
|
||||||
|
return expectPromise;
|
||||||
});
|
});
|
||||||
it('should let types flow through the Promise', function (done) {
|
it('should let types flow through the Promise', function () {
|
||||||
let myString = 'someString';
|
let myString = 'someString';
|
||||||
let myDeferred = q.defer();
|
let myDeferred = q.defer();
|
||||||
myDeferred.promise.then(x => {
|
let expectPromise = smartchai_1.expect(myDeferred.promise).to.eventually.equal('someString');
|
||||||
should(x).equal('someString');
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
myDeferred.resolve(myString);
|
myDeferred.resolve(myString);
|
||||||
|
return expectPromise;
|
||||||
|
});
|
||||||
|
it('should promisify a callback', function () {
|
||||||
|
let myPromisified = q.promisify(myCallback);
|
||||||
|
let expectPromise = smartchai_1.expect(myPromisified('hi')).to.eventually.equal('hi');
|
||||||
|
return expectPromise;
|
||||||
|
});
|
||||||
|
it('should map callbacks', function () {
|
||||||
|
let inputArray = ['hi', 'awesome'];
|
||||||
|
let myPromisified = q.promisify(myCallback);
|
||||||
|
let expectPromise = smartchai_1.expect(q.map(inputArray, myPromisified)).to.eventually.deep.equal(inputArray);
|
||||||
|
return expectPromise;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGVzdC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbInRlc3QudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLHdCQ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BQUMsQ0FBQyxFQUFFLENBQUMsVUFBVSxDQUFDLEtBQUssQ0FBQyxJQUFJLENBQUMsQ0FBQTtRQUN6RSxNQUFNLENBQUMsYUFBYSxDQUFBO0lBQ3hCLENBQUMsQ0FBQyxDQUFBO0lBRUYsRUFBRSxDQUFDLHNCQUFzQixFQUFFO1FBQ3ZCLElBQUksVUFBVSxHQUFHLENBQUMsSUFBSSxFQUFFLFNBQVMsQ0FBQyxDQUFBO1FBQ2xDLElBQUksYUFBYSxHQUFHLENBQUMsQ0FBQyxTQUFTLENBQUMsVUFBVSxDQUFDLENBQUE7UUFDM0MsSUFBSSxhQUFhLEdBQUcsa0JBQU0sQ0FBQyxDQUFDLENBQUMsR0FBRyxDQUFDLFVBQVUsRUFBRSxhQUFhLENBQUMsQ0FBQyxDQUFDLEVBQUUsQ0FBQyxVQUFVLENBQUMsSUFBSSxDQUFDLEtBQUssQ0FBQyxVQUFVLENBQUMsQ0FBQTtRQUNqRyxNQUFNLENBQUMsYUFBYSxDQUFBO0lBQ3hCLENBQUMsQ0FBQyxDQUFBO0FBQ04sQ0FBQyxDQUFDLENBQUEifQ==
|
36
test/test.ts
36
test/test.ts
@ -1,24 +1,38 @@
|
|||||||
import 'typings-test'
|
import 'typings-test'
|
||||||
|
|
||||||
import * as should from 'should'
|
import { expect } from 'smartchai'
|
||||||
import * as q from '../dist/index'
|
import * as q from '../dist/index'
|
||||||
|
|
||||||
describe('smartq', function() {
|
let myCallback = (someValue1: string, cb?) => {
|
||||||
it('should return a Deferred for .defer()', function(done) {
|
cb(null, someValue1)
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('smartq', function () {
|
||||||
|
it('should return a Deferred for .defer()', function () {
|
||||||
let myDeferred = q.defer()
|
let myDeferred = q.defer()
|
||||||
myDeferred.promise.then(() => {
|
let expectPromise = expect(myDeferred.promise).to.eventually.be.fulfilled
|
||||||
done()
|
|
||||||
})
|
|
||||||
myDeferred.resolve()
|
myDeferred.resolve()
|
||||||
|
return expectPromise
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should let types flow through the Promise', function(done) {
|
it('should let types flow through the Promise', function () {
|
||||||
let myString = 'someString'
|
let myString = 'someString'
|
||||||
let myDeferred = q.defer<string>()
|
let myDeferred = q.defer<string>()
|
||||||
myDeferred.promise.then(x => {
|
let expectPromise = expect(myDeferred.promise).to.eventually.equal('someString')
|
||||||
should(x).equal('someString')
|
|
||||||
done()
|
|
||||||
})
|
|
||||||
myDeferred.resolve(myString)
|
myDeferred.resolve(myString)
|
||||||
|
return expectPromise
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should promisify a callback', function () {
|
||||||
|
let myPromisified = q.promisify(myCallback)
|
||||||
|
let expectPromise = expect(myPromisified('hi')).to.eventually.equal('hi')
|
||||||
|
return expectPromise
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should map callbacks', function() {
|
||||||
|
let inputArray = ['hi', 'awesome']
|
||||||
|
let myPromisified = q.promisify(myCallback)
|
||||||
|
let expectPromise = expect(q.map(inputArray, myPromisified)).to.eventually.deep.equal(inputArray)
|
||||||
|
return expectPromise
|
||||||
})
|
})
|
||||||
})
|
})
|
@ -1,5 +1,7 @@
|
|||||||
import 'typings-global'
|
import 'typings-global'
|
||||||
|
|
||||||
|
import * as typedPromisify from 'typed-promisify'
|
||||||
|
|
||||||
export interface IResolve<T> {
|
export interface IResolve<T> {
|
||||||
(value?: T | Promise<T>): void
|
(value?: T | Promise<T>): void
|
||||||
}
|
}
|
||||||
@ -37,3 +39,7 @@ export let resolvedPromise = <T>(value?: T): Promise<T> => {
|
|||||||
export let rejectedPromise = (err) => {
|
export let rejectedPromise = (err) => {
|
||||||
return Promise.reject(err)
|
return Promise.reject(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export let promisify = typedPromisify.promisify
|
||||||
|
export let map = typedPromisify.map
|
||||||
|
export let _try = typedPromisify._try
|
||||||
|
Loading…
Reference in New Issue
Block a user