Skip to content

deferFn always resolves false #87

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

Closed
wyqydsyq opened this issue Aug 23, 2019 · 2 comments
Closed

deferFn always resolves false #87

wyqydsyq opened this issue Aug 23, 2019 · 2 comments

Comments

@wyqydsyq
Copy link

wyqydsyq commented Aug 23, 2019

deferFn should resolve the value of it's underlying promise, however it actually always resolves false:

	const { data, run, setData } = useAsync({
		initialValue: defaultContext,
		deferFn: customerLogin,
	});

	if (doLogin) {
		run(customerId).then(console.log);
	}

Logs false.

I would expect to get the object resolved by the Promise that customerLogin returns.

I want to be able to extract the value from the promise deferFn resolves, then call setData with the resolved response so the component re-renders with the new data e.g.

	const { data, run, setData } = useAsync({
		initialValue: defaultContext,
		deferFn: customerLogin,
	});

	if (doLogin) {
		run(customerId).then(customer => setData(customer));
	}

I need to use deferFn because I only want to run the promise conditionally, promiseFn (which does return the resolved value as expected) executes automatically so wouldn't work here, but I also want to get the data resolved from the deferred promise as well.

@ghengeveld
Copy link
Member

This is actually going to change in the next release, and should resolve your issue. See #83

In a nutshell:

  • run no longer returns anything
  • a new promise property is introduced, which properly resolves/rejects as expected

Regardless, you shouldn't need this at all. If you run a deferFn, it's fulfilled value will automatically become the data. You don't need setData to do that.

@ghengeveld
Copy link
Member

I'm going to close this because it should be resolved by either relying on run to update data automatically, or by using the newly introduced promise property returned by useAsync.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants