Skip to content

Instantly share code, notes, and snippets.

@Jungwoo-An
Last active November 26, 2019 01:51
Show Gist options
  • Save Jungwoo-An/c126892703d0e253f5f4d58b7c6ae378 to your computer and use it in GitHub Desktop.
Save Jungwoo-An/c126892703d0e253f5f4d58b7c6ae378 to your computer and use it in GitHub Desktop.
combine resolved promise
export function debounceUntilResolve(func: any) {
let isDone = true;
return async (...args: any[]) => {
if (!isDone) {
return;
}
isDone = false;
try {
await func(...args);
} finally {
isDone = true;
}
};
}
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