Need code that works? Grab a snippet. Made something cool? Share it back. Simple as that.
async function retryPromise<T>( fn: () => Promise<T>, maxAttempts = 3, // Retry 3 times by default delay = 1000 // Retry after 1 second by default ): Promise<T> { let attempts = 0; while (attempts < maxAttempts) { try { return await fn(); } catch { attempts++; if (attempts >= maxAttempts) throw new Error("Max attempts reached."); await new Promise((res) => setTimeout(res, delay)); } } throw new Error("Unexpected error."); } // Mock function async function fetchData() { if (Math.random() > 0.7) return "Success!"; throw new Error("Failed!"); } retryPromise(fetchData).then(console.log); // Success!
Explore the powerful features that make SnipNest an awesome platform.
Easily fork, edit, and submit code snippets. Share your work with the community in just a few clicks.
function chunkArray(array, size) { return Array.from({ length: Math.ceil(array.length / size) }, (_, i) => array.slice(i * size, i * size + size) ); } const numbers = [1, 2, 3, 4, 5, 6, 7]; chunkArray(numbers, 3); // [[1, 2, 3], [4, 5, 6], [7]]
Quickly filter through a vast collection of code snippets. Get the perfect code for your project every time.
Access and insert your saved snippets directly within your code editor. Boost your productivity without leaving your workspace.
function chunkArray(array, size) { return Array.from({ length: Math.ceil(array.length / size) }, (_, i) => array.slice(i * size, i * size + size) ); } const numbers = [1, 2, 3, 4, 5, 6, 7]; chunkArray(numbers, 3); // [[1, 2, 3], [4, 5, 6], [7]]
Help improve the platform by contributing snippets and collaborating with other developers.