A concurrency primitive is reusable functionality for sharing resources or coordinating concurrent execution paths. It limits undesirable orderings so concurrent code reaches a correct result. section 16.0 section 16.6
Common concurrency primitives make different timing guarantees:
- Queue or linearize calls so one asynchronous action runs at a time and tasks complete in order. section 16.6
- DroppingQueue keeps only a bounded number of pending tasks, skipping stale work when newer work arrives. section 16.9
- Cut waits for a specified number of timelines to finish before continuing. section 17.10
- JustOnce wraps an action so repeated calls produce an effect only on the first call. section 17.15
Serializing cart-total updates
A queue accepts cart states, removes the oldest pending item, and runs its worker only when no earlier item is active. After the worker finishes, it starts the next item. Wrapping this logic in Queue(worker) separates reusable coordination from application-specific work. section 16.4 section 16.6
function Cut(num, callback) { #1
var num_finished = 0; #2
return function() {
num_finished += 1; #3
if(num_finished === num)
callback(); #4
};
}
Inspect the compact Cut implementation: it closes over a completion counter and invokes the callback when the final timeline reports completion. section 17.10
Timeline diagrams help diagnose timing bugs before choosing a primitive. The design process is incremental: isolate shared coordination, encode the desired ordering or repetition, then generalize the reusable portion. section 16.11 section 17.17

