Skip to content
Myr

async.md

This file

Path

docs/async.md

56 lines · 2.6 KiB · Markdown

Last changed by

docs: updated sponsor linksMichel Weststratebefore adoptionf97d037

History of this file

Take it

Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46

lines 1–6why, one step back
1---
2id: async3title: Async producers & createDraft / finishDraft4sidebar_label: Async produce / createDraft5---6

docs: updated sponsor linksMichel Weststratebefore adoptionf97d037

lines 7–9why, one step back
7<center>
8<div data-ea-publisher="immerjs" data-ea-type="image" class="horizontal bordered"></div>9</center>

Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46

line 10why, one step back
10

Added egghead.io video embedsMichel Weststratebefore adoptiona866760

lines 11–14why, one step back
11<details>
12 <summary style="color: white; background:#c200c2;padding:5px;margin:5px;border-radius:2px">egghead.io lesson 11: Creating _async_ producers (and why you shouldn’t)</summary>13 <br>14 <div style="padding:5px;">

docs: fix video player scrolling on chrome, fixes #520Michel Weststratebefore adoptionfbae585

line 15why, one step back
15 <iframe style="border: none;" width=760 height=427 scrolling="no" src="https://egghead.io/lessons/react-write-asynchronous-producers-in-immer-and-why-you-shouldn-t/embed" ></iframe>

Added egghead.io video embedsMichel Weststratebefore adoptiona866760

lines 16–19why, one step back
16 </div>
17 <a style="font-style:italic;padding:5px;margin:5px;" href="https://egghead.io/lessons/react-write-asynchronous-producers-in-immer-and-why-you-shouldn-t">Hosted on egghead.io</a>18</details>19

Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46

lines 20–56why, one step back
20It 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) will return a promise if the producer is async. Example:
2122```javascript23import produce from "immer"2425const user = {26 name: "michel",27 todos: []28}2930const loadedUser = await produce(user, async function(draft) {31 draft.todos = await (await window.fetch("http://host/" + draft.name)).json()32})33```3435_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._3637## `createDraft` and `finishDraft`3839`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:4041```javascript42import {createDraft, finishDraft} from "immer"4344const user = {45 name: "michel",46 todos: []47}4849const draft = createDraft(user)50draft.todos = await (await window.fetch("http://host/" + draft.name)).json()51const loadedUser = finishDraft(draft)52```5354Note: `finishDraft` takes a `patchListener` as second argument, which can be used to record the patches, similarly to `produce`.5556_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._