Javascript Examples

Page 2 of 9

Browse Javascript examples

javascriptSnippet

Hedged Requests Race Replicas

Start a backup replica after a short tail delay and race it against the primary, keeping the first answer so slow replicas stop dominating latency.

javascriptSnippet

Lazy Async Init Single Promise

The first call starts one init promise that every caller shares, and a rejection clears it so the next call retries instead of failing forever.

javascriptSnippet

concat Merges Without Flattening

concat returns a fresh array merging arguments and spreads values shallowly: nested arrays stay nested and objects are shared by reference with the source.

javascriptSnippet

Async Iteration Over Emitter Events

events.on exposes an EventEmitter as an async iterable; when the passed AbortSignal fires the loop raises AbortError, so consumers can bound how much they read.

javascriptSnippet

forEach Ignores Async Callbacks

forEach calls async callbacks without awaiting them, so the loop ends before the work does; for...of awaits sequentially and Promise.all waits in parallel.