2. Asynchronous JavaScript & Browser APIs

Practice Lab 1: Debounced Search, State Closures & Parallel Async

Put Modules 1 and 2 into action: implement a closure-based debounce() utility, an immutable store update, and parallel Promise.allSettled error-resilient aggregation.

2 min readUpdated 2026-12-01
In This Lesson
  • Implement a reusable closure-based debounce(fn, waitMs) helper
  • Combine immutable spread updates with nullish coalescing safely
  • Aggregate partial successes and failures cleanly using Promise.allSettled

Reading about closures and async patterns only locks in once you implement them yourself. Complete both drills below—one synchronous ES6 data/closure drill and one live async debounce UI drill!

Drill 1: Immutable Inventory Store (??, Spread & Closure)

Implement createStore(initialItems) returning { add(item), updateQty(id, delta), getItems() } where:

  • add appends { id, name, qty: item.qty ?? 1 } without mutating prior arrays.
  • updateQty(id, delta) updates only the matching item immutably (never dropping below 0).
  • getItems() returns a shallow/safe snapshot so callers can't mutate internal state directly.
JavaScriptDrill 1: Closure Store & Immutable Updates
Output

Click “Run Code” to execute this snippet right in your browser.

Drill 2: Live debounce() Helper + Promise.allSettled()

In this live Web Sandbox, complete debounce(fn, delayMs) using a closure timerId + clearTimeout + setTimeout, and run it against the input field!

HTML / CSS / JSDrill 2: Live Debounced Input in Browser
Live Browser Preview

Lab 1 Completion Checklist

0/3 done