[Vuejs]-How can I get these promises to resolve in order

0πŸ‘

βœ…

As explained in the documentation, signUserUp should return a promise, in order for to chain it with this.$store.dispatch('signUserUp', userProfile).then(...). Firebase supports promises where possible. It’s possible to chain once() and flatten promise chain with async..await:

async function signUserUp({commit}, payload) {
  try {
    ...  
    const snapshot = await firebase.database().ref('users/' + slug).once('value');
    ...
    const cred = await firebase.auth().createUserWithEmailAndPassword(payload.email, payload.password);
    ...
  } catch (error) {
    commit('setLoading', false)
    commit('setError', error)
  }
}

0πŸ‘

If you need to wait on the asynchronous code in your action to complete then your action should return a Promise.

Probably the simplest way* to make this work with your current code is to wrap it in a new Promise.

signUserUp({commit}, payload) {
    return new Promise(function(resolve, reject) {
        /* existing function body */
    });
}

Then add calls to resolve() when the asynchronous code has completed. (For example, at this point: console.log('user profile uploaded') or after one of the errors.) Once you call the resolve callback it will invoke the then handler in your signup method. You can reject the Promise as well using the reject callback. In your case however it looks like you want the action to handle the error itself and not propagate it.

*Since it looks like the firebase API uses promises you could also attempt to return the result of the firebase functions.

Leave a comment