async.mdx
This file
Path
website/docs/async.mdx
59 lines · 2.0 KiB
Last changed by
chore: Merge branch 'unional-prettier'Michel Weststratebefore adoption37eaf8f
History of this fileTake it
The walk stopped after 200 commits: lines older than 07f5cc9 point at it.
chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 1–2why, one step back1---2id: asyncMerge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 3–4why, one step back3title: createDraft / finishDraft4sidebar_label: createDraft / finishDraftchore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 5–12why, one step back5---67<center>8 <div9 data-ea-publisher="immerjs"10 data-ea-type="image"11 className="horizontal bordered"12 ></div>chore: Merge branch 'unional-prettier'Michel Weststratebefore adoption37eaf8f
lines 13–14why, one step back13</center>14<details>chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 15–37why, one step back15 <summary className="egghead-summary">16 egghead.io lesson 11: Creating <b>async</b> producers (and why you17 shouldn’t)18 </summary>19 <br />20 <div>21 <iframe22 width="760"23 height="427"24 scrolling="no"25 src="https://egghead.io/lessons/react-write-asynchronous-producers-in-immer-and-why-you-shouldn-t/embed"26 ></iframe>27 </div>28 <a29 className="egghead-link"30 href="https://egghead.io/lessons/react-write-asynchronous-producers-in-immer-and-why-you-shouldn-t"31 >32 Hosted on egghead.io33 </a>34</details>3536## `createDraft` and `finishDraft`37Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 38why, one step back38`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.chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
line 39why, one step back39Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 40–41why, one step back40Beyond that, `createDraft` / `finishDraft` could be used to express async updates to drafts:41chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 42–53why, one step back42```javascript43import {createDraft, finishDraft} from "immer"4445const user = {46 name: "michel",47 todos: []48}4950const draft = createDraft(user)51draft.todos = await (await window.fetch("http://host/" + draft.name)).json()52const loadedUser = finishDraft(draft)53```Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 54–55why, one step back5455Note: The above is an anti-pattern! First fetch data instead, then draft the `user`. Otherwise updates to user that happen during the async process, would be "missed" by the draft.chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 56–59why, one step back5657Note: `finishDraft` takes a `patchListener` as second argument, which can be used to record the patches, similarly to `produce`.5859_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._