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> ) { retu...Mar 23, 2022 · 5 min readabdellahcodes.com
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> ) { retu...Mar 20, 2022 · 5 min readabdellahcodes.com
Promises: run any promise with a timeoutA promise has two states: either pending or settled (resolved or rejected). The user has no control over the time it takes from going from the first state to the second. Which makes it harder to bail out on a certain promise when it takes too long in...Mar 18, 2022 · 3 min readabdellahcodes.com
Promises: async/await vs .then(), how we got here and why I use bothHow we got here Promises marked a huge turning point in async js, they enabled a new type of control flow that saved us from callback hell. But some people found that calling .then() multiple times was too much, too callbacky. Then after a while, we ...1MMar 16, 2022 · 3 min readabdellahcodes.com