Back to Exercises
Async / Promises
20 Exercises Available
async/await, parallel calls, retries — production-grade patterns.
1.Easy
Sleep (delay)
Create a function `sleep(ms)` that resolves after ms milliseconds.
Sample Output:
await sleep(1000) => (resolves after 1 second) await sleep(500) => (resolves after 0.5 seconds)
View Solution
2.Medium
Run in Parallel (Promise.all)
Function `parallel(tasks)` banao jahan tasks array of functions (returning promises) ho. Saare parallel run karo and results return karo.
Sample Output:
await parallel([() => Promise.resolve(1), () => Promise.resolve(2)]) => [1, 2] await parallel([() => fetchUser(), () => fetchPosts()]) => [userData, postsData]
View Solution
3.Hard
Retry Wrapper (3 tries)
Create a function `retry(fn, tries=3)`. Retry on failure, throw on last fail.
Sample Output:
await retry(() => fetchAPI(), 3) // If succeeds on 1st try: returns result // If fails 2 times, succeeds on 3rd: returns result // If all 3 fail: throws last error
View Solution
4.Hard
Timeout Promise
Function `withTimeout(promise, ms)` banao. ms ke andar resolve na ho to reject with Error("Timeout").
Sample Output:
await withTimeout(fetchAPI(), 5000)
// If API completes in < 5s: returns API result
// If API takes > 5s: throws Error("Timeout")View Solution
5.Medium
Sequential Runner
Create a function `series(tasks)` that runs tasks one-by-one (promise returning).
Sample Output:
await series([() => Promise.resolve(1), () => Promise.resolve(2)]) => [1, 2] await series([() => fetchUser(), () => fetchPosts()]) => [userData, postsData] (sequential)
View Solution
6.Hard
Promise.allSettled Implementation
Create a function `allSettled(promises)` that waits for all promises to complete (both success/failure).
Example: allSettled([Promise.resolve(1), Promise.reject(2)]) => [{status: "fulfilled", value: 1}, {status: "rejected", reason: 2}]
Sample Output:
await allSettled([Promise.resolve(1), Promise.reject(2)]) => [{status: "fulfilled", value: 1}, {status: "rejected", reason: 2}]View Solution
7.Medium
Promise.race Implementation
Create a function `race(promises)` that returns result of first promise to resolve/reject.
Example: race([slowPromise, fastPromise]) => fastPromise result
Sample Output:
await race([slowPromise, fastPromise]) => (fastPromise result) // Returns result of first resolved/rejected promise
View Solution
8.Medium
Promise Chain with Error Recovery
Function `withRecovery(promise, recoveryFn)` banao jo promise fail ho to recovery function call kare.
Example: withRecovery(fetchAPI(), () => getCachedData())
Sample Output:
await withRecovery(fetchAPI(), () => getCachedData()) // If fetchAPI fails, returns cached data
View Solution
9.Hard
Batch Process Array with Concurrency Limit
Function `batchProcess(items, processor, concurrency)` banao jo items ko process kare with concurrency limit.
Example: batchProcess([1,2,3,4], async (x) => x*2, 2) → max 2 parallel
Sample Output:
await batchProcess([1,2,3,4], async (x) => x*2, 2) => [2, 4, 6, 8] // Processes max 2 items at a time
View Solution
10.Hard
Create Resolvable Promise
Function `createResolvable()` banao jo ek promise return kare jiska resolve/reject externally control ho.
Example: const {promise, resolve, reject} = createResolvable();
Sample Output:
const {promise, resolve, reject} = createResolvable();
resolve(42); await promise => 42View Solution
11.Hard
Promise Queue (sequential processing)
Function `createQueue()` banao jo promises ko queue me add kare aur sequentially process kare.
Example: queue.add(() => fetchAPI1()); queue.add(() => fetchAPI2());
Sample Output:
const queue = createQueue(); queue.add(() => fetchAPI1()); queue.add(() => fetchAPI2()); // Processes sequentially
View Solution
12.Easy
Promise Delay (with value)
Function `delay(ms, value)` banao jo ms ke baad value resolve kare.
Example: delay(1000, "hello") => resolves after 1s with "hello"
Sample Output:
await delay(1000, "hello") => "hello" (after 1 second)
View Solution
13.Medium
Promise Timeout Wrapper
Function `withTimeout(promise, ms, timeoutValue)` banao jo promise ko timeout kare with default value.
Example: withTimeout(fetchAPI(), 5000, null)
Sample Output:
await withTimeout(fetchAPI(), 5000, null) // If timeout: rejects with null
View Solution
14.Hard
Retry with Exponential Backoff
Function `retryWithBackoff(fn, maxAttempts, baseDelay)` banao jo retry kare with exponential backoff.
Example: retryWithBackoff(fetchAPI, 3, 100) → delays: 100ms, 200ms, 400ms
Sample Output:
await retryWithBackoff(fetchAPI, 3, 100) // Retries with delays: 100ms, 200ms, 400ms
View Solution
15.Hard
Promise Pool (limit concurrent)
Function `createPool(size)` banao jo concurrent promises ko limit kare.
Example: pool.add(() => fetchAPI1()); pool.add(() => fetchAPI2()); max size concurrent
Sample Output:
const pool = createPool(2); pool.add(() => fetchAPI1()); pool.add(() => fetchAPI2()); // Max 2 concurrent
View Solution
16.Easy
Promise Finally Implementation
Function `finally(promise, fn)` banao jo promise complete hone pe (success/failure) function call kare.
Example: finally(fetchAPI(), () => cleanup())
Sample Output:
await finally(fetchAPI(), () => cleanup()) // Calls cleanup regardless of success/failure
View Solution
17.Medium
Promise Map (transform array)
Function `promiseMap(arr, fn, concurrency)` banao jo array ko promise function se map kare with concurrency.
Example: promiseMap([1,2,3], async (x) => x*2, 2)
Sample Output:
await promiseMap([1,2,3], async (x) => x*2, 2) => [2, 4, 6] // Maps with max 2 concurrent
View Solution
18.Medium
Promise Filter (async condition)
Function `promiseFilter(arr, fn)` banao jo array ko async condition se filter kare.
Example: promiseFilter([1,2,3], async (x) => x > 1)
Sample Output:
await promiseFilter([1,2,3], async (x) => x > 1) => [2, 3] // Filters with async condition
View Solution
19.Hard
Promise Reduce (async accumulator)
Function `promiseReduce(arr, fn, initial)` banao jo array ko async reducer se reduce kare.
Example: promiseReduce([1,2,3], async (acc, x) => acc + x, 0)
Sample Output:
await promiseReduce([1,2,3], async (acc, x) => acc + x, 0) => 6 // Reduces with async accumulator
View Solution
20.Hard
Cancelable Promise
Function `createCancelable(promise)` banao jo cancelable promise return kare.
Example: const {promise, cancel} = createCancelable(fetchAPI()); cancel()
Sample Output:
const {promise, cancel} = createCancelable(fetchAPI());
cancel(); await promise => rejects with "Cancelled"View Solution