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
|
||||
import * as q from 'smartq'
|
||||
|
||||
// Deferred
|
||||
// -----------------------------------------------
|
||||
let myAsyncFunction = (): Promise<string> => {
|
||||
let done = q.defer<string>() // returns your typical Deferred object
|
||||
setTimeout(() => {
|
||||
@ -42,6 +44,9 @@ let myAsyncFunction2 = async () => {
|
||||
|
||||
myAsyncFunction2();
|
||||
|
||||
|
||||
// Resolved and Rejected promises
|
||||
// ------------------------------------------------
|
||||
q.resolvedPromise(`I'll get logged to console soon`)
|
||||
.then(x => {
|
||||
console.log(x)
|
||||
@ -55,6 +60,23 @@ q.rejectedPromise(`what a lovely error message`)
|
||||
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)
|
||||
|
3
dist/index.d.ts
vendored
3
dist/index.d.ts
vendored
@ -1,4 +1,5 @@
|
||||
import 'typings-global';
|
||||
import * as typedPromisify from 'typed-promisify';
|
||||
export interface IResolve<T> {
|
||||
(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.
|
||||
*/
|
||||
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";
|
||||
require("typings-global");
|
||||
const typedPromisify = require("typed-promisify");
|
||||
class Deferred {
|
||||
constructor() {
|
||||
this.promise = new Promise((resolve, reject) => {
|
||||
@ -24,4 +25,6 @@ exports.resolvedPromise = (value) => {
|
||||
exports.rejectedPromise = (err) => {
|
||||
return Promise.reject(err);
|
||||
};
|
||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUEsMEJBQXVCO0FBVXZCO0lBSUk7UUFDSSxJQUFJLENBQUMsT0FBTyxHQUFHLElBQUksT0FBTyxDQUFJLENBQUMsT0FBTyxFQUFFLE1BQU07WUFDMUMsSUFBSSxDQUFDLE9BQU8sR0FBRyxPQUFPLENBQUE7WUFDdEIsSUFBSSxDQUFDLE1BQU0sR0FBRyxNQUFNLENBQUE7UUFDeEIsQ0FBQyxDQUFDLENBQUE7SUFDTixDQUFDO0NBQ0o7QUFWRCw0QkFVQztBQUVVLFFBQUEsS0FBSyxHQUFHO0lBQ2YsTUFBTSxDQUFDLElBQUksUUFBUSxFQUFLLENBQUE7QUFDNUIsQ0FBQyxDQUFBO0FBRUQ7O0dBRUc7QUFDUSxRQUFBLGVBQWUsR0FBRyxDQUFJLEtBQVM7SUFDdEMsTUFBTSxDQUFDLE9BQU8sQ0FBQyxPQUFPLENBQUMsS0FBSyxDQUFDLENBQUE7QUFDakMsQ0FBQyxDQUFBO0FBRUQ7O0dBRUc7QUFDUSxRQUFBLGVBQWUsR0FBRyxDQUFDLEdBQUc7SUFDN0IsTUFBTSxDQUFDLE9BQU8sQ0FBQyxNQUFNLEN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QSJ9
|
@ -18,11 +18,11 @@
|
||||
},
|
||||
"homepage": "https://gitlab.com/pushrocks/smartq#README",
|
||||
"dependencies": {
|
||||
"typed-promisify": "^0.3.0",
|
||||
"typings-global": "^1.0.14"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/should": "^8.1.30",
|
||||
"should": "^11.1.2",
|
||||
"smartchai": "^1.0.1",
|
||||
"typings-test": "^1.0.3"
|
||||
}
|
||||
}
|
||||
|
33
test/test.js
33
test/test.js
@ -1,23 +1,34 @@
|
||||
"use strict";
|
||||
require("typings-test");
|
||||
const should = require("should");
|
||||
const smartchai_1 = require("smartchai");
|
||||
const q = require("../dist/index");
|
||||
let myCallback = (someValue1, cb) => {
|
||||
cb(null, someValue1);
|
||||
};
|
||||
describe('smartq', function () {
|
||||
it('should return a Deferred for .defer()', function (done) {
|
||||
it('should return a Deferred for .defer()', function () {
|
||||
let myDeferred = q.defer();
|
||||
myDeferred.promise.then(() => {
|
||||
done();
|
||||
});
|
||||
let expectPromise = smartchai_1.expect(myDeferred.promise).to.eventually.be.fulfilled;
|
||||
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 myDeferred = q.defer();
|
||||
myDeferred.promise.then(x => {
|
||||
should(x).equal('someString');
|
||||
done();
|
||||
});
|
||||
let expectPromise = smartchai_1.expect(myDeferred.promise).to.eventually.equal('someString');
|
||||
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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGVzdC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbInRlc3QudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLHdCQUFxQjtBQUVyQixpQ0FBZ0M7QUFDaEMsbUNBQWtDO0FBRWxDLFFBQVEsQ0FBQyxRQUFRLEVBQUU7SUFDZixFQUFFLENBQUMsdUNBQXVDLEVBQUUsVUFBUyxJQUFJO1FBQ3JELElBQUksVUFBVSxHQUFHLENBQUMsQ0FBQyxLQUFLLEVBQUUsQ0FBQTtRQUMxQixVQUFVLENBQUMsT0FBTyxDQUFDLElBQUksQ0FBQztZQUNwQixJQUFJLEVBQUUsQ0FBQTtRQUNWLENBQUMsQ0FBQyxDQUFBO1FBQ0YsVUFBVSxDQUFDLE9BQU8sRUFBRSxDQUFBO0lBQ3hCLENBQUMsQ0FBQyxDQUFBO0lBRUYsRUFBRSxDQUFDLDJDQUEyQyxFQUFFLFVBQVMsSUFBSTtRQUN6RCxJQUFJLFFBQVEsR0FBRyxZQUFZLENBQUE7UUFDM0IsSUFBSSxVQUFVLEdBQUcsQ0FBQyxDQUFDLEtBQUssRUFBVSxDQUFBO1FBQ2xDLFVBQVUsQ0FBQyxPQUFPLENBQUMsSUFBSSxDQUFDLENBQUM7WUFDckIsTUFBTSxDQUFDLENBQUMsQ0FBQyxDQUFDLEtBQUssQ0FBQyxZQUFZLENBQUMsQ0FBQTtZQUM3QixJQUFJLEVBQUUsQ0FBQTtRQUNWLENBQUMsQ0FBQyxDQUFBO1FBQ0YsVUFBVSxDQUFDLE9BQU8sQ0FBQyxRQUFRLENBQUMsQ0FBQTtJQUNoQyxDQUFDLENBQUMsQ0FBQTtBQUNOLENBQUMsQ0FBQyxDQUFBIn0=
|
||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZX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UMsQ0FBQyxDQUFBO0lBRUYsRUFBRSxDQUFDLDZCQUE2QixFQUFFO1FBQzlCLElBQUksYUFBYSxHQUFHLENBQUMsQ0FBQyxTQUFTLENBQUMsVUFBVSxDQUFDLENBQUE7UUFDM0MsSUFBSSxhQUFhLEdBQUcsa0JBQU0sQ0FBQyxhQUFhLENBQUMsSUFBSSxDQUFDLENBQUMsQ0FBQyxFQUFFLENBQUMsVUFBVSxDQUFDLEtBQUssQ0FBQyxJQUFJLENBQUMsQ0FBQTtRQUN6RSxNQUFNLENBQUMsYUFBYSxDQUFBO0lBQ3hCLENBQUMsQ0FBQyxDQUFBO0lBRUYsRUFBRSxDQUFDLHNCQUFzQixFQUFFO1FBQ3ZCLElBQUksVUFBVSxHQUFHLENBQUMsSUFBSSxFQUFFLFNBQVMsQ0FBQyxDQUFBO1FBQ2xDLElBQUksYUFBYSxHQUFHLENBQUMsQ0FBQyxTQUFTLENBQUMsVUFBVSxDQUFDLENBQUE7UUFDM0MsSUFBSSxhQUFhLEdBQUcsa0JBQU0sQ0FBQyxDQUFDLENBQUMsR0FBRyxDQUFDLFVBQVUsRUFBRSxhQUFhLENBQUMsQ0FBQyxDQUFDLEVBQUUsQ0FBQyxVQUFVLENBQUMsSUFBSSxDQUFDLEtBQUssQ0FBQyxVQUFVLENBQUMsQ0FBQTtRQUNqRyxNQUFNLENBQUMsYUFBYSxDQUFBO0lBQ3hCLENBQUMsQ0FBQyxDQUFBO0FBQ04sQ0FBQyxDQUFDLENBQUEifQ==
|
38
test/test.ts
38
test/test.ts
@ -1,24 +1,38 @@
|
||||
import 'typings-test'
|
||||
|
||||
import * as should from 'should'
|
||||
import { expect } from 'smartchai'
|
||||
import * as q from '../dist/index'
|
||||
|
||||
describe('smartq', function() {
|
||||
it('should return a Deferred for .defer()', function(done) {
|
||||
let myCallback = (someValue1: string, cb?) => {
|
||||
cb(null, someValue1)
|
||||
}
|
||||
|
||||
describe('smartq', function () {
|
||||
it('should return a Deferred for .defer()', function () {
|
||||
let myDeferred = q.defer()
|
||||
myDeferred.promise.then(() => {
|
||||
done()
|
||||
})
|
||||
let expectPromise = expect(myDeferred.promise).to.eventually.be.fulfilled
|
||||
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 myDeferred = q.defer<string>()
|
||||
myDeferred.promise.then(x => {
|
||||
should(x).equal('someString')
|
||||
done()
|
||||
})
|
||||
let expectPromise = expect(myDeferred.promise).to.eventually.equal('someString')
|
||||
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 * as typedPromisify from 'typed-promisify'
|
||||
|
||||
export interface IResolve<T> {
|
||||
(value?: T | Promise<T>): void
|
||||
}
|
||||
@ -37,3 +39,7 @@ export let resolvedPromise = <T>(value?: T): Promise<T> => {
|
||||
export let rejectedPromise = (err) => {
|
||||
return Promise.reject(err)
|
||||
}
|
||||
|
||||
export let promisify = typedPromisify.promisify
|
||||
export let map = typedPromisify.map
|
||||
export let _try = typedPromisify._try
|
||||
|
Loading…
x
Reference in New Issue
Block a user