July 2018
Beginner
236 pages
5h 34m
English
Computed properties have the special ability to recover from errors thrown during computation. Rather than bailing out immediately, they catch and hold on to the error. It is only when you try to read from a computed-property, that it will rethrow the error. This gives you the chance to recover by resetting some state and getting back to some default state.
The following example is straight from the MobX docs, and aptly demonstrates the error recovery:
import { observable, computed } from 'mobx';const x = observable.box(3);const y = observable.box(1);const divided = computed(() => { if (y.get() === 0) { throw new Error('Division by zero'); } return x.get() / y.get();});divided.get(); // returns 3y.set(0);
Read now
Unlock full access