-
Notifications
You must be signed in to change notification settings - Fork 36
Description
I'm not sure what the reasoning behind this is, but it appears that .all() creates a new Scope when it's called, but isn't documented as such. This can cause surprising behavior: an example from some code I was writing tonight looks something like this:
const ops: Operation<int>[] = [/* some array of operations that should run simultaneously */];
const tasks = yield* all(ops.map((op) => spawn(() => op))); /* throws error: Halted, surprising */
for (const task of tasks) {
const result = yield* task;
/* do something async with result, say:*/
yield* sleep(result);
}I believe this is because the operations all start, returning a task, but when the Scope from all is destroyed as a result of it returning to the parent, the scope for all the spawned operations is also dead.
If I just wanted to run them simultaneously and deal with the results, I could just use .all directly and skip this, but in this case I want to run all the operations simultaneously and then do something else asynchronous with them.
In any case, this is workaround-able by simply emulating what .all does without creating a new scope, but it seems strange to me that the new scope is needed in the first place, as it seems to be just an extra call wrapping the whole function.
Is this intentional behavior?