# resource

```ts
function resource<T>(operation: (provide: Provide<T>) => Operation<void>): Operation<T>
```

Define an Effection [resource](https://frontside.com/effection/docs/resources)

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
```javascript
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](https://frontside.com/effection/api/v3/Provide.md)&lt;T&gt;) => [Operation](https://frontside.com/effection/api/v3/Operation.md)&lt;void&gt;


the operation defining the lifecycle of the resource
### Return Type


[Operation](https://frontside.com/effection/api/v3/Operation.md)&lt;T&gt;


an operation yielding the resource

[View code](https://github.com/thefrontside/effection/tree/effection-v3.6.1/lib/instructions.ts#L244)
