Skip to content
Myr

docs: async and createDraft / finishDraft

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Michel Weststrate · committed by Michel Weststrate · parent 5479196 · Browse files · 1 file+40 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+40 added−0 removed

  • other:+40 added−0 removed

1 file+40 added−0 removed

modified:readme.md+40 added
333 unmodified lines
334For a more in-depth study, see [Distributing patches and rebasing actions using Immer](https://medium.com/@mweststrate/distributing-state-changes-using-snapshots-patches-and-actions-part-2-2f50d8363988)335336Tip: Check this trick to [compress patches](https://medium.com/@david.b.edelstein/using-immer-to-compress-immer-patches-f382835b6c69) produced over time.337338## Async producers339340It is allowed to return Promise objects from recipes. Or, in other words, to use `async / await`. This can be pretty useful for long running processes, that only produce the new object once the promise chain resolves. Note that `produce` itself (even in the curried form) itself will return a promise if the producer is async. Example:341342```javascript343import produce from "immer"344345const user = {346 name: "michel",347 todos: []348}349350const loadedUser = await produce(users, async function(draft) {351 user.todos = await window.fetch("http://host/" + draft.name).json()352})353```354355_Warning: please note that the draft shouldn't be 'leaked' from the async process and stored else where. The draft will still be revoked as soon as the async process completes._356357## `createDraft` and `finishDraft`358359`createDraft` and `finishDraft` are two low-level functions that are mostly useful for libraries that build abstractions on top of immer. It avoids the need to always create a function in order to work with drafts. Instead, one can create a draft, modify it, and at some time in the future finish the draft, in which case the next immutable state will be produced. We could for example rewrite our above example as:360361```javascript362import { createDraft, finishDraft } from "immer"363364const user = {365 name: "michel",366 todos: []367}368369const draft = createDraft(user)370const draft.todos = await window.fetch("http://host/" + draft.name).json()371const loadedUser = finishDraft(draft)372```373374Note: `finishDraft` takes a `patchListener` as second argument, which can be used to record the patches, similarly to `produce`.375376_Warning: in general, we recommend to use `produce` instead of the `createDraft` / `finishDraft` combo, `produce` is less error prone in usage, and more clearly separates the concepts of mutability and immutability in your code base._377378## Returning data from producers379380It is not needed to return anything from a producer, as Immer will return the (finalized) version of the `draft` anyway. However, it is allowed to just `return draft`.381
371 unmodified lines