fix(core): update
This commit is contained in:
parent
f7492c4656
commit
5aecc44ad7
33
ts/index.ts
33
ts/index.ts
@ -42,21 +42,21 @@ export class Deferred<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export let defer = <T>() => {
|
export const defer = <T>() => {
|
||||||
return new Deferred<T>();
|
return new Deferred<T>();
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new resolved promise for the provided value.
|
* Creates a new resolved promise for the provided value.
|
||||||
*/
|
*/
|
||||||
export let resolvedPromise = <T>(value?: T): Promise<T> => {
|
export const resolvedPromise = <T>(value?: T): Promise<T> => {
|
||||||
return Promise.resolve(value);
|
return Promise.resolve(value);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new rejected promise for the provided reason.
|
* Creates a new rejected promise for the provided reason.
|
||||||
*/
|
*/
|
||||||
export let rejectedPromise = err => {
|
export const rejectedPromise = (err) => {
|
||||||
return Promise.reject(err);
|
return Promise.reject(err);
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -64,16 +64,31 @@ interface IAsyncFunction<T> {
|
|||||||
(someArg: T): Promise<T>;
|
(someArg: T): Promise<T>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export let map = async <T>(inputArg: T[], functionArg: IAsyncFunction<T>) => {
|
/**
|
||||||
let promiseArray: Promise<any>[] = [];
|
* accepts an array of inputs and a function that accepts the input.
|
||||||
let resultArray = [];
|
* runs all items with the function and returns the result array when all items have run
|
||||||
for (let item of inputArg) {
|
* @param inputArg
|
||||||
let promise: Promise<any> = functionArg(item);
|
* @param functionArg
|
||||||
|
*/
|
||||||
|
export const map = async <T>(inputArg: T[], functionArg: IAsyncFunction<T>) => {
|
||||||
|
const promiseArray: Promise<any>[] = [];
|
||||||
|
const resultArray = [];
|
||||||
|
for (const item of inputArg) {
|
||||||
|
const promise: Promise<any> = functionArg(item);
|
||||||
promiseArray.push(promise);
|
promiseArray.push(promise);
|
||||||
promise.then(x => {
|
promise.then((x) => {
|
||||||
resultArray.push(x);
|
resultArray.push(x);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
await Promise.all(promiseArray);
|
await Promise.all(promiseArray);
|
||||||
return resultArray;
|
return resultArray;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const timeoutWrap = (ms, promise) => {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
setTimeout(() => {
|
||||||
|
reject(new Error('timeout'));
|
||||||
|
}, ms);
|
||||||
|
promise.then(resolve, reject);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user