BREAKING CHANGE(remove util dependency and promisify functionality): update
This commit is contained in:
parent
e42061047a
commit
93f426cce0
14
test/test.ts
14
test/test.ts
@ -1,10 +1,6 @@
|
|||||||
import { expect, tap } from 'tapbundle';
|
import { expect, tap } from 'tapbundle';
|
||||||
import * as q from '../ts/index';
|
import * as q from '../ts/index';
|
||||||
|
|
||||||
let myCallback = (someValue1: string, cb?) => {
|
|
||||||
cb(null, someValue1);
|
|
||||||
};
|
|
||||||
|
|
||||||
tap.test('should return a Deferred for .defer()', async () => {
|
tap.test('should return a Deferred for .defer()', async () => {
|
||||||
let myDeferred = q.defer();
|
let myDeferred = q.defer();
|
||||||
let expectPromise = expect(myDeferred.promise).to.eventually.be.fulfilled;
|
let expectPromise = expect(myDeferred.promise).to.eventually.be.fulfilled;
|
||||||
@ -20,16 +16,10 @@ tap.test('should let types flow through the Promise', async () => {
|
|||||||
return expectPromise;
|
return expectPromise;
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.test('should promisify a callback', async () => {
|
|
||||||
let myPromisified = q.promisify(myCallback);
|
|
||||||
let expectPromise = expect(myPromisified('hi')).to.eventually.equal('hi');
|
|
||||||
return await expectPromise;
|
|
||||||
});
|
|
||||||
|
|
||||||
tap.test('should map callbacks', async () => {
|
tap.test('should map callbacks', async () => {
|
||||||
let inputArray = ['hi', 'awesome'];
|
let inputArray = ['hi', 'awesome'];
|
||||||
let myPromisified = q.promisify(myCallback);
|
const myPromisified = async (myInput) => { return myInput };
|
||||||
let expectPromise = expect(q.map(inputArray, myPromisified)).to.eventually.deep.equal(inputArray);
|
const expectPromise = expect(q.map(inputArray, myPromisified)).to.eventually.deep.equal(inputArray);
|
||||||
return expectPromise;
|
return expectPromise;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
11
ts/index.ts
11
ts/index.ts
@ -1,5 +1,3 @@
|
|||||||
import * as util from 'util';
|
|
||||||
|
|
||||||
export interface IResolve<T> {
|
export interface IResolve<T> {
|
||||||
(value?: T | Promise<T>): void;
|
(value?: T | Promise<T>): void;
|
||||||
}
|
}
|
||||||
@ -42,14 +40,15 @@ export let rejectedPromise = err => {
|
|||||||
return Promise.reject(err);
|
return Promise.reject(err);
|
||||||
};
|
};
|
||||||
|
|
||||||
export let promisify = util.promisify;
|
interface IAsyncFunction<T> {
|
||||||
|
(someArg: T):Promise<T>
|
||||||
|
}
|
||||||
|
|
||||||
export let map = async <T>(inputArg: T[], functionArg) => {
|
export let map = async <T>(inputArg: T[], functionArg: IAsyncFunction<T> ) => {
|
||||||
let promisifedFunction = promisify(functionArg);
|
|
||||||
let promiseArray: Promise<any>[] = [];
|
let promiseArray: Promise<any>[] = [];
|
||||||
let resultArray = [];
|
let resultArray = [];
|
||||||
for (let item of inputArg) {
|
for (let item of inputArg) {
|
||||||
let promise: Promise<any> = promisifedFunction(item);
|
let promise: Promise<any> = functionArg(item);
|
||||||
promiseArray.push(promise);
|
promiseArray.push(promise);
|
||||||
promise.then(x => {
|
promise.then(x => {
|
||||||
resultArray.push(x);
|
resultArray.push(x);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user