docs: fixes
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 3b8b39f · Browse files · 1 file·+5 added−5 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+5 added−5 removed
- other:+5 added−5 removed
Provenance
- Commit
9c498e1- Tree
7144b6a- Author
Michel Weststrate <mweststrate@gmail.com>- Committer
Michel Weststrate <mweststrate@gmail.com>- Parent
3b8b39f
1 file changed+5 added−5 removed
modified:readme.md+5 added-5 removed
335 unmodified lines336 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: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) will return a promise if the producer is async. Example:341 342 ```javascript343 import produce from "immer"344 1 unmodified line346 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()350+const loadedUser = await produce(user, async function(draft) {351+ user.todos = await (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._2 unmodified lines358 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"362+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()370+draft.todos = await (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`.378 unmodified lines