Adding structured concurrency to JavaScript This repository exists for me and anyone else interested to explore directions for adding structured concurrency to JavaScript. What is structured concurrency? "Structured concurrency" refers to a way of writing concurrent programs such that: child tasks are bound to a lexical scope scope doesn't exit until children complete or shut down including waiting for cleanup to finish if canceled (usually) errors in one child task cancel other tasks What does it look like in other languages? Java has StructuredTaskScope, designed for use with its try-with-resources statement (which is its way of doing explicit resource management, like JS's using statement). It looks like this: try (var scope = new StructuredTaskScope.ShutdownOnFailure()) { var userTask = scope.fork(() -> fetchUser(userId)); var ordersTask = scope.fork(() -> fetchOrders(userId)); var recsTask = scope.fork(() -> fetchRecommendations(userId)); scope.join(); scope.throwIfFailed(); // if we get here, all subtasks have completed return "user=%s, orders=%s, recs=%s".formatted( userTask.get(), ordersTask.get(), recsTask.get() ); } catch (Exception e) { // if we get here, all subtasks have been fully shut down return "error: failed to load"; } These tasks run in lightweight threads, and rather than having explicit async/await points there are various blocking library methods which check whether their thread has been interrupted, throwing InterruptedException if so. Python has asyncio.TaskGroup, designed for use with its async with statement (which is its way of doing explicit resource management). It looks like this: try: async with asyncio.TaskGroup() as tg: user_task = tg.create_task(fetch_user(userId)) orders_task = tg.create_task(fetch_orders(userId)) recs_task = tg.create_task(fetch_recommendations(userId)) # if we get here, all subtasks have completed return (f"user={user_task.result()}, " f"orders={orders_task.result()}, " f"recs={recs_task.result()}") except Excep...
First seen: 2026-03-24 19:36
Last seen: 2026-03-25 16:51