Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In ReactNative, which Async Await method is better in these two and why?

verifyUser which awaits verifyUserSignInSuccess which awaits userSnapshot which awaits user

Here in these two functions, which will be more effective in terms of correctness, memory, time for ReactNative app :

export function verifyUser() {
  return async dispatch => {
    dispatch(verifyUserSignInRequest());
    try {
      const user = await firebase.auth().onAuthStateChanged();

        if (user) {
          let userRef = "/user/" + user.uid;
          const userSnapshot = await firebase
            .database()
            .ref(userRef)
            .once("value");
          dispatch(verifyUserSignInSuccess(userSnapshot.val()));
        } else {
          dispatch(verifyUserSignInFailure(USER_NOT_SIGNED_IN));
        }
    } catch (e) {
      dispatch(verifyUserSignInFailure(e.message));
    }
  };
}

Or the Nested Async Await :

export function verifyUser() {
  return async dispatch => {
    dispatch(verifyUserSignInRequest());
    try {
      await firebase.auth().onAuthStateChanged(async user => {
        if (user) {
          let userRef = "/user/" + user.uid;
          await firebase
            .database()
            .ref(userRef)
            .once("value")
            .then( () => {
              dispatch(verifyUserSignInSuccess(userSnapshot.val()));
            });
        } else {
          dispatch(verifyUserSignInFailure(USER_NOT_SIGNED_IN));
        }
      });
    } catch (e) {
      dispatch(verifyUserSignInFailure(e.message));
    }
  };
}
like image 219
Sriram C G Avatar asked Dec 11 '17 09:12

Sriram C G


Video Answer


2 Answers

According to the documentation, the onAuthStateChanged() function returns

The unsubscribe function for the observer.

So you can just:

var unsubscribe = firebase.auth().onAuthStateChanged((user) {
    // handle it for changes signed in, signed out, or when the user's ID token changed in situations such as token expiry or password change 
});

And then:

unsubscribe(); for registering the for observer.

onAuthStateChanged is a Observer which calls the observer when users were signed in, signed out, or when the user's ID token changed in situations such as token expiry or password change . so the second one is the best solution . every login or change .

` let userRef = "/user/" + user.uid;
          await firebase
            .database()
            .ref(userRef)
            .once("value")
            .then( () => {
              dispatch(verifyUserSignInSuccess(userSnapshot.val()));
            });
        } else {
          dispatch(verifyUserSignInFailure(USER_NOT_SIGNED_IN));
        }` 

that is correct to cross check is user is valid or not .i dont't thinks there is memory comparison is required.

like image 142
TheCodeTalker Avatar answered Sep 29 '22 15:09

TheCodeTalker


Time - Since all your async functions need to run one after the other whichever method u use async/await or promise chaining or a mix up of both will take same time only.

Correctness - Both are correct logically and will work the same. But async/await is the latest addition to JS to solve the problem of promise chaining. Promise chaining leaves the code hard to read. Better U stick to async/await.For situations where u need to run two async functions parallelly, use await Promise.all() etc. In the end, it's your personal preference.

Memory? - I have no idea on that

Read this book its free on github which contains details on promises, async functions, async/await etc. https://github.com/getify/You-Dont-Know-JS

like image 21
Ravi Raj Avatar answered Sep 29 '22 15:09

Ravi Raj