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 * as q from '../ts/index';
|
||||
|
||||
let myCallback = (someValue1: string, cb?) => {
|
||||
cb(null, someValue1);
|
||||
};
|
||||
|
||||
tap.test('should return a Deferred for .defer()', async () => {
|
||||
let myDeferred = q.defer();
|
||||
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;
|
||||
});
|
||||
|
||||
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 () => {
|
||||
let inputArray = ['hi', 'awesome'];
|
||||
let myPromisified = q.promisify(myCallback);
|
||||
let expectPromise = expect(q.map(inputArray, myPromisified)).to.eventually.deep.equal(inputArray);
|
||||
const myPromisified = async (myInput) => { return myInput };
|
||||
const expectPromise = expect(q.map(inputArray, myPromisified)).to.eventually.deep.equal(inputArray);
|
||||
return expectPromise;
|
||||
});
|
||||
|
||||
|
11
ts/index.ts
11
ts/index.ts
@ -1,5 +1,3 @@
|
||||
import * as util from 'util';
|
||||
|
||||
export interface IResolve<T> {
|
||||
(value?: T | Promise<T>): void;
|
||||
}
|
||||
@ -42,14 +40,15 @@ export let rejectedPromise = 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) => {
|
||||
let promisifedFunction = promisify(functionArg);
|
||||
export let map = async <T>(inputArg: T[], functionArg: IAsyncFunction<T> ) => {
|
||||
let promiseArray: Promise<any>[] = [];
|
||||
let resultArray = [];
|
||||
for (let item of inputArg) {
|
||||
let promise: Promise<any> = promisifedFunction(item);
|
||||
let promise: Promise<any> = functionArg(item);
|
||||
promiseArray.push(promise);
|
||||
promise.then(x => {
|
||||
resultArray.push(x);
|
||||
|
Loading…
x
Reference in New Issue
Block a user