Skip to content

Commit

Permalink
📄 Documentation for withResolvers()
Browse files Browse the repository at this point in the history
withResolvers() is a very handy API that is part of the vanilla JS
stack and also part of v4. This adds jsdocs to the with resolvers and
also adds an entry in the async rosetta stone.
  • Loading branch information
cowboyd committed Jan 3, 2025
1 parent 14fb40a commit dab567d
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions www/docs/async-rosetta-stone.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,39 @@ function* main() {
};
```

## `Promise.withResolvers()` \<=> `withResolvers()`

Both `Promise` and `Operation` can be constructed ahead of time without needing to begin the process that will resolve it. To do this with
a `Promise`, use the `Promise.withResolvers()` function:

```ts
async function main() {
let { promise, resolve } = Promise.withResolvers();

setTimeout(resolve, 1000);

await promise;

console.log("done!")
}
```

In effection:

```ts
import { withResolvers } from "effection";

function* main() {
let { operation, resolve } = withResolvers();

setTimeout(resolve, 1000);

yield* operation;

console.log("done!");
};
```

## `for await` \<=> `for yield* each`

Loop over an AsyncIterable with `for await`:
Expand Down

0 comments on commit dab567d

Please sign in to comment.