pikax / vue-composable

Vue composition-api composable components. i18n, validation, pagination, fetch, etc. +50 different composables

Home Page:https://pikax.me/vue-composable/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

why usePromise.promise.value is null?

wizardnet972 opened this issue · comments

I try to get the promise because I want to attach hook to the then callback. but I get undefined.

Is there a way to get the promise immediately after usePromise execute?

function timeout(ms) {
   return new Promise((resolve) => setTimeout(resolve, ms));
 }

 const api = usePromise(() => Promise.all([timeout(12000).then(() => ({ some: 'data'}))]));

 console.log({ p: api.promise.value }); // <---- undefined

code in codesandbox

Currently the usePromise is only executed after exec() is called.

The reason for that is usePromise allows you to exec with arguments

const {exec} = usePromise((page:number)=> api.fetchPage(page));

exec(1);
exec(2);

But I'm thinking in changing the behaviour to execute immediately and add a options argument to allow to do lazy. But on the example above the exec would be called with undefined

My thoughts currently are:

usePromise(()=>api()); // immediately 
usePromise(()=>api(), {immediately: false}); // lazy

usePromise((...args)=>api(...args)); // lazy - if contains args 
usePromise((...args)=>api(...args), {immediately: true}); // immediately 

My concern is the different behaviour of having args and not having them, but if I exec them immediately might cause some problems.

Released 1.0.0-dev.16

Update the documentation to reflect the changes

I decided to make the usePromise to exec straight away, if the factory has argument, a warning will be log.

Also added a sugar for lazy promises usePromiseLazy

Let me know if you encounter any other issue 👍

@pikax thank you :)