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

verifyUser , which expects verifyUserSignInSuccess , which expects userSnapshot , which expects user

Here in these two functions that will be more efficient in terms of correctness, memory, time for a ReactNative application:

 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 expected lively asynchronous wait:

 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)); } }; } 
+5
source share
2 answers

According to the documentation , the onAuthStateChanged() function returns

Unsubscribe function for the observer.

So you can simply:

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

And then:

unsubscribe(); to register an observer.

onAuthStateChanged is an observer that calls an observer when users are signed up, logged off, or when the token of the user ID has changed in situations such as expiration of the token or password change. therefore, the second is the best solution. every entry 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)); }` 

which is correct for cross-checking whether it is valid or not. I don't think memory comparison is required.

+2
source

Time. Since all your asynchronous functions must be run one after the other, whatever method you use, asynchronous / waiting or promising a chain, or mixing both will be done at the same time.

Correctness - both are logically correct and will work the same. But async / await is the latest addition to JS to solve the promise chain problem. The promise chain leaves the code difficult to read. Better U stick with async / await. In situations where you need to run two functions of asynchronous use in parallel, use await Promise.all() , etc. In the end, these are your personal preferences.

Memory? - I have no idea about this.

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

+2
source

Source: https://habr.com/ru/post/1273974/


All Articles