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
Provenance
- Commit
3b8b39f- Tree
78e2957- Author
Michel Weststrate <mweststrate@gmail.com>- Committer
Michel Weststrate <mweststrate@gmail.com>- Parent
5479196
1 file changed+40 added−0 removed
modified:readme.md+40 added
333 unmodified lines334 For 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)335 336 Tip: Check this trick to [compress patches](https://medium.com/@david.b.edelstein/using-immer-to-compress-immer-patches-f382835b6c69) produced over time.337 338+## Async producers339+340+It 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:341+342+```javascript343+import produce from "immer"344+345+const user = {346+ name: "michel",347+ todos: []348+}349+350+const loadedUser = await produce(users, async function(draft) {351+ user.todos = await window.fetch("http://host/" + draft.name).json()352+})353+```354+355+_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._356+357+## `createDraft` and `finishDraft`358+359+`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:360+361+```javascript362+import { createDraft, finishDraft } from "immer"363+364+const user = {365+ name: "michel",366+ todos: []367+}368+369+const draft = createDraft(user)370+const draft.todos = await window.fetch("http://host/" + draft.name).json()371+const loadedUser = finishDraft(draft)372+```373+374+Note: `finishDraft` takes a `patchListener` as second argument, which can be used to record the patches, similarly to `produce`.375+376+_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._377+378 ## Returning data from producers379 380 It 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