Parallel promise execution: Beyound Promise.all()

In this post, I will talk about running promises in parallel and optimizing for the lowest waiting time possible. When you have a bunch of promises that need to execute sequentially, you just run them in a sequence using .then() or awaits: getUser() .then(user => getPosts(user.id)) .then(posts => use(posts)); // Or let user = await getUser(); let posts = await getPosts(user.id); use(posts); But when they don’t depend on each other like the posts that need the user id, you can run them in parallel using Promise....

March 31, 2022 · 2 min · 373 words · Abdellah Hariti

Use Typescript generics for a type safe `setTimeout` and `setInterval`

TLDR; Here’s the code: type Handler = ((...args: any[]) => any) | string; function safeSetTimeout<F extends Handler>( handler: F, timeout?: number, ...args: F extends string ? any[] : Parameters<F extends string ? never : F> ) { return setTimeout(handler, timeout, ...args); } If you understand everything in the following snippet, you don’t have much to gain from this post. But you might want check out the practical snippet at the end of this post....

March 20, 2022 · 5 min · 956 words · Abdellah Hariti