From dab567de6831f3ace1e238176b3ec0e3f7f3e5ad Mon Sep 17 00:00:00 2001 From: Charles Lowell Date: Fri, 20 Dec 2024 13:42:21 -0600 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=84=20Documentation=20=20for=20`withRe?= =?UTF-8?q?solvers()`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- www/docs/async-rosetta-stone.mdx | 33 ++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/www/docs/async-rosetta-stone.mdx b/www/docs/async-rosetta-stone.mdx index dec62170..c34a5d92 100644 --- a/www/docs/async-rosetta-stone.mdx +++ b/www/docs/async-rosetta-stone.mdx @@ -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`: