AsyncAutoResetEvent Class
Package: @esfx/async-autoresetevent
Represents a synchronization event that, when signaled, resets automatically after releasing a single waiting asynchronous operation.
Declaration
export declare class AsyncAutoResetEvent
Examples
The following example shows how to use AsyncAutoResetEvent to release one asynchronous operation at a time whenever the user presses Enter. Because the first event is initially signaled, the first operation is released immediately. This resets the signaled state, causing the remaining operations to pause until the event is once again signaled.
Note
The following example is derived from https://docs.microsoft.com/en-us/dotnet/api/system.threading.autoresetevent?view=net-6.0#examples
import { AsyncAutoResetEvent } from "@esfx/async-autoresetevent";
import { delay } from "@esfx/async-delay";
import * as readline from "readline";
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
const question = (text: string) => new Promise<string>(resolve => rl.question(text, resolve));
const first = new AsyncAutoResetEvent(/*initialState*/ true);
const second = new AsyncAutoResetEvent(/*initialState*/ false);
async function main() {
await question(`Press Enter to start three asynchronous operations.`);
const promises: Promise<void>[] = [];
for (let i = 0; i < 3; i++) {
promises.push(asyncOperation(`operation #${i + 1}`));
}
await delay(250);
for (let i = 0; i < 2; i++) {
await question(`Press Enter to release an operation.`);
first.set();
await delay(250);
}
console.log("All operations are now waiting on the second event.");
for (let i = 0; i < 3; i++) {
await question(`Press Enter to release an operation.`);
second.set();
await delay(250);
}
await Promise.all(promises);
}
async function asyncOperation(name: string) {
console.log(`${name} is waiting on the first event.`);
await first.wait();
console.log(`${name} was released from the first event.`);
console.log(`${name} is waiting on the second event.`);
await second.wait();
console.log(`${name} was released from the second event.`);
console.log(`${name} is complete.`);
}
await main();
Remarks
The AsyncAutoResetEvent and AsyncManualResetEvent classes are used to provide signaling between concurrent asynchronous operations.
An asynchronous operation can wait until an event is signaled by awaiting the result of wait(cancelable). This causes the operation to pause until the event becomes signaled. Calling set() will signal and release a single waiting operation, immediately returning the event to the non-signaled state. If there are no operations currently waiting on the event, the event will remain signaled until the next call to wait(cancelable).
Calling reset() resets the event to the non-signaled state.
Constructors
constructor(initialState)
Initializes a new instance of the AutoResetEvent class.
Declaration
constructor(initialState?: boolean);
Parameters
Methods
reset()
Sets the state of the event to nonsignaled, causing asynchronous operations to pause.
Declaration
reset(): void;
Returns
set()
Sets the state of the event to signaled, resolving at most one waiting Promise. The event is then automatically reset.
Declaration
set(): boolean;
Returns
true
if the operation successfully resolved a waiting Promise; otherwise, false
.
wait(cancelable)
Asynchronously waits for the event to become signaled.
Declaration
wait(cancelable?: Cancelable): Promise<void>;
Parameters
Returns