Skip to content
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

Add async mutation support #134

Merged
merged 5 commits into from Nov 21, 2019
Merged

Add async mutation support #134

merged 5 commits into from Nov 21, 2019

Conversation

shuding
Copy link
Member

@shuding shuding commented Nov 19, 2019

With this change, the data parameter of mutate can be a promise, which returns the data or throws an error:

mutate(key, Promise<Data>)

It can simplify scenarios like

const newData = await update(...)   // some POST APIs return the updated data
mutate('/api/data', newData, false) // disable the revalidation here

to:

mutate('/api/data', update(...)) // POST + mutate w/o revalidation

Related: #93.

cacheSet(key, data)
let data, error

if (_data && typeof _data.then === 'function') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can await a non-promise too. Not sure if there is a performance issue though

Suggested change
if (_data && typeof _data.then === 'function') {
const data = await _data;

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will defer the execution order (await non-promise values), similar to process.nextTick. But the major reason is that for promises (which should be mutation requests), we don't have to revalidate again after the request in most cases.

Copy link
Contributor

@pacocoursey pacocoursey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great feature idea! The actual code looks good to me.

README.md Outdated Show resolved Hide resolved
Co-Authored-By: Paco <34928425+pacocoursey@users.noreply.github.com>
@shuding shuding merged commit ed26efc into master Nov 21, 2019
@shuding shuding deleted the mutate-promise branch November 21, 2019 04:02
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

Successfully merging this pull request may close these issues.

None yet

3 participants