← Back to list

Llamadas asíncronas en MobX State Tree

¿Qué son y como se utilizan las `function *` generators ? ¿Se pueden sustituir por async / await?

Sejas in JS School · 2018-09-25 12:11 · 163 claps · 0.8 min read
#javascript #mobx #mst #react #async
Open on Medium ↗
Wiki topics: LLM · Large Language Models 🌐 · Web Development

Llamadas asíncronas en MobX State Tree

¿Qué son y como se utilizan las function * generators ? ¿Se pueden sustituir por async / await?

Hay dos opciones:

  1. Async/Await o promesas, pero en el callback debemos llamar a otro action auxiliar.
  2. Function generators utilizando flow y sustituyendo los await por yield

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


메타데이터
post_id
d347ea8c2dd3
slug
llamadas-asíncronas-en-mobx-state-tree-d347ea8c2dd3
url
https://medium.com/js-school/llamadas-as%C3%ADncronas-en-mobx-state-tree-d347ea8c2dd3
canonical_url
https://medium.com/js-school/llamadas-as%C3%ADncronas-en-mobx-state-tree-d347ea8c2dd3
author_url
https://medium.com/@sejas
status
ok
fetched_at
2026-07-29 23:38:26