DEV Community

Aurélien Delogu
Aurélien Delogu

Posted on • Edited on

2 1

Code Snippet: How to avoid calling a function twice until its task has finished

const busyCallbacks = new Set()
export async function busy(callback : () => any) : Promise<any> {
if (busyCallbacks.has(callback)) {
return
}
busyCallbacks.add(callback)
const value = await callback()
busyCallbacks.delete(callback)
return value
}
view raw busy.ts hosted with ❤ by GitHub

Top comments (1)

Collapse
 
pyrsmk profile image
Aurélien Delogu

For example, this is especially useful for applications that wait for callbacks that usually have a tiny waiting time, to avoid losing time implementing routines to block/unblock a button (which requires styling, possible more HTML tags, additional JS code, etc...).

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs