Skip to content
Myr

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 file

Take it

The walk stopped after 200 commits: lines older than ced4563 point at it.

fix: protect isDraftable against undefined constructor (#969)Brian Hungbefore adoptionced4563

lines 1–2why, one step back
1---
2id: async

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

lines 3–4why, one step back
3title: createDraft / finishDraft
4sidebar_label: createDraft / finishDraft

fix: protect isDraftable against undefined constructor (#969)Brian Hungbefore adoptionced4563

lines 5–12why, one step back
5---
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 back
13</center>
14<details>

fix: protect isDraftable against undefined constructor (#969)Brian Hungbefore adoptionced4563

lines 15–37why, one step back
15 <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`37

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 38why, one step back
38`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.

fix: protect isDraftable against undefined constructor (#969)Brian Hungbefore adoptionced4563

line 39why, one step back
39

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

lines 40–41why, one step back
40Beyond that, `createDraft` / `finishDraft` could be used to express async updates to drafts:
41

fix: protect isDraftable against undefined constructor (#969)Brian Hungbefore adoptionced4563

lines 42–53why, one step back
42```javascript
43import {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 back
54
55Note: 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.

fix: protect isDraftable against undefined constructor (#969)Brian Hungbefore adoptionced4563

lines 56–59why, one step back
56
57Note: `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._