Newer version available
You’re viewing the API reference for version 3.0.3, but the latest version is 3.1.0. The latest version include may important updates and fixes.
View Latest Versionfunction resource
thefrontside/effectionfunction resource<T>(operation: (provide: Provide<T>) => Operation<void>): Operation<T>
Define an Effection resource
Resources are a type of operation that passes a value back to its caller
while still allowing that operation to run in the background. It does this
by invoking the special provide()
operation. The caller pauses until the
resource operation invokes provide()
at which point the caller resumes with
passed value.
provide()
suspends the resource operation until the caller passes out of
scope.
Examples
Example 1
function useWebSocket(url) {
return resource(function*(provide) {
let socket = new WebSocket(url);
yield* once(socket, 'open');
try {
yield* provide(socket);
} finally {
socket.close();
yield* once(socket, 'close');
}
})
}
await main(function*() {
let socket = yield* useWebSocket("wss://example.com");
socket.send("hello world");
});
Type Parameters
T
Parameters
operation: (provide: Provide<T>) => Operation<void>
the operation defining the lifecycle of the resource
Return Type
Operation<T>
an operation yielding the resource