Replies: 9 comments
-
Thanks for reporting.
Does |
Beta Was this translation helpful? Give feedback.
-
Here's derive using async/await https://codesandbox.io/s/derive-with-promises-yx2fkq?file=/src/App.js and here's subscribe using async/await https://codesandbox.io/s/subscribe-with-promises-7pokmq I think the key difference is that
Whether |
Beta Was this translation helpful? Give feedback.
-
https://codesandbox.io/s/derive-with-promises-yx2fkq?file=/src/App.js https://codesandbox.io/s/subscribe-with-promises-7pokmq
So, option 1 is to leave it as is. Option 2 could be removing I'd be appreciated if you work on docs/wikis. |
Beta Was this translation helpful? Give feedback.
-
I think option 1 is superior to option 2. I've found a very nice use for
The one alternative option is having specific logic to allow async/await for watch and treat an async function as if it were returning void. |
Beta Was this translation helpful? Give feedback.
-
I thought this would be confusing and people might misuse. Here's the diff that could be acceptable: diff --git a/src/utils/watch.ts b/src/utils/watch.ts
index 14fbd63..bc41d30 100644
--- a/src/utils/watch.ts
+++ b/src/utils/watch.ts
@@ -2,7 +2,7 @@ import { subscribe } from '../vanilla'
type Cleanup = () => void
type WatchGet = <T extends object>(proxyObject: T) => T
-type WatchCallback = (get: WatchGet) => Cleanup | void | undefined
+type WatchCallback = (get: WatchGet) => Cleanup | void | Promise<void>
type WatchOptions = {
sync?: boolean
}
@@ -75,7 +75,7 @@ export function watch(
})
// If there's a cleanup, we add this to the cleanups set
- if (cleanupReturn) {
+ if (typeof cleanupReturn === 'function') {
cleanups.add(cleanupReturn)
}
} finally { Note: I dropped Would you like to open a PR with this fix and a test to cover it? |
Beta Was this translation helpful? Give feedback.
-
Just a note here, Vue allows async watch functions. Though it also doesn't utilize any kind of watch "cleanup". There are definitely good use cases. |
Beta Was this translation helpful? Give feedback.
-
Hey I would like to help. this is my first open source contribution :) |
Beta Was this translation helpful? Give feedback.
-
@NeriRos Yes, please go ahead. Note that I'm not yet confident if the outcome is mergeable. |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
https://codesandbox.io/s/breaking-watch-ct246n
If you attempt to use async/await in watch it'll break the cleanup cycle. I added a quick test locally and fooled around with it and can avoid the issue with something along these lines:
However, I'm not sure that's the desired behavior either as that would also mean you can't run a cleanup function using async/await without additional logic, but maybe that's ok.
Derive handles this scenario already of working with promises, so it feels a little odd that watch is the odd function out when it comes to promises. I suppose one workaround is doing a manual subscribe dance around the promises and their subscriptions.
Beta Was this translation helpful? Give feedback.
All reactions