-
-
Notifications
You must be signed in to change notification settings - Fork 64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to chain thens in usePromise? #414
Comments
When you do What you can do is passing the function to the exec: const useApi = () => {
const api = usePromise(p => getItems().then(p), { lazy: true });
return api;
};
//setup
exec(async r => {
console.log({ r, loading });
await timeout(20 * 1000);
}); |
@pikax thanks for your answer. But what I think of is to use I think about something like this to catch the error or the promise. instead of try..catch..finally .
This is mean something like Promise/finally:
so here you will get the catch when error, and somebody can hook into the original promise. (and the finally always happens) |
I don't think you can hook to the original promise. export function timeout(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
const getItems = () => timeout(1000).then(() => [{ id: 1 }, { id: 2 }]);
function myPromise(p) {
const loading = ref(true);
const promise = Promise.resolve(p).finally(() => (loading.value = false));
return {
promise,
loading
};
}
export default defineComponent({
name: "App",
setup() {
const promise = myPromise(getItems());
promise.promise.then(async x => {
console.log("resolved", x);
await timeout(2000);
console.log("rrr ", promise.loading.value); // this is always false
});
return promise;
}
}); That's a javascript behaviour, if you can provide a working example of hooking into a promised already created I really appreciate it :) |
@pikax Here what I suggest. I rewrite the What do you think about it? can I send PR?
|
That The value is only resolved once, no matter how many times you call |
How to chain
.then
s functions and adjust theloading
property to be true after all the thens complete?I have a function
getItems
that return array with ids after 20 seconds (like http).and I call this function via
usePromise
:const useApi = () => {
const api = usePromise(() => getItems(), { lazy: true });
return api;
};
In the setup function I call to useApi and pass the
loading
to the template, and I get the r with the data.But what if I want to add other function without change the
useApi
and I wantloading
to not change until all of thethen
complete.I want to wait another 20 seconds. but after the first
then
complete it setloading
toture
.@pikax There is something I can do to make it works?
my example on codesandbox.io
The text was updated successfully, but these errors were encountered: