¿Qué son y como se utilizan las `function *
` generators ? ¿Se pueden sustituir por async / await?
Hay dos opciones:
Async/Await
o promesas, pero en el callback debemos llamar a otroaction
auxiliar.- Function
generators
utilizando flow y sustituyendo losawait
poryield
Con MobX State Tree (MST), es mejor utilizar function generetors
, porque nos permiten mantenernos en el contexto de la action y así evitar la creación de las actions auxiliares.
types
.model({
email: "",
password: "",
userLogged: false
})
.actions( self => ({
login: flow(function*(){
try {
let user = yield firebaseAuth.signInAndRetrieveDataWithEmailAndPassword(self.email, self.password)
self.userLogged = true
} catch (error) {
//catch error
}
})
})
)
Por el contrario, utilizando async await
, perdemos el contexto y MobX no puede identificar que el callback
se está ejecutando dentro de un action
.
Por eso obtenemos este error:
the object is protected and can only be modified by using an action
Doc oficial: Creating asynchronous actions