Last active
November 26, 2019 01:51
-
-
Save Jungwoo-An/c126892703d0e253f5f4d58b7c6ae378 to your computer and use it in GitHub Desktop.
combine resolved promise
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function debounceUntilResolve(func: any) { | |
let isDone = true; | |
return async (...args: any[]) => { | |
if (!isDone) { | |
return; | |
} | |
isDone = false; | |
try { | |
await func(...args); | |
} finally { | |
isDone = true; | |
} | |
}; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function combine<T>(promises: Promise<T>[]): Promise<T[]> { | |
return new Promise((resolve) => { | |
let cnt = 0; | |
const result: T[] = []; | |
const done = () => { | |
cnt += 1; | |
if (cnt === promises.length) { | |
resolve(result); | |
} | |
}; | |
const complete = (data) => { | |
result.push(data); | |
}; | |
promises.forEach(promise => promise.then(complete).finally(done)); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment