Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6287

Async function doesn't resolve when i use AsyncStorage on React Native

$
0
0

Hello guys i'm using ignite react native but i have a problem when using async storage, the async function to call api doesn't resolve. When i use Reactotron i can see the api call was made successfully but the process doesn't go to the next step.this instruction doesn't resolve its still waiting !

const result = await authApi.getCurrentUser(self.environment.token)

here is the code.

export class Environment {  constructor() {    // create each service    if (__DEV__) {      // dev-only services      this.reactotron = new ReactotronDev()    }    this.api = new Api()  }  async setup() {    // allow each service to setup    this.token = await loadString("token")    if (__DEV__) {      await this.reactotron.setup()    }    await this.api.setup()  }  /**   * Reactotron is only available in dev.   */  reactotron: typeof ReactotronDev  /**   * Our api.   */  api: Api  /**   * User token.   */  token: string}
async getCurrentUser(token: string): Promise<GetCurrentUserResult> {    try {      // make the api call      this.api.apisauce.setHeader("Authorization", `Bearer ${token}`)      const response: ApiResponse<any> = await this.api.apisauce.get("/current-user")      // the typical ways to die when calling an api      if (!response.ok) {        const problem = getGeneralApiProblem(response)        console.tron.log(response)        if (problem) return { ...problem, messages: response.data }      }      const currentUser = response.data      return { kind: "ok", currentUser }    } catch (e) {      __DEV__ && console.tron.log(e.message)      return { kind: "bad-data" }    }  }
    loadCurrentUser: async () => {      self.setLoading(true)      const rootStore: RootStore = getRoot(self)      const authApi = new AuthApi(self.environment.api)      const result = await authApi.getCurrentUser(self.environment.token)      console.tron.log(result)      if (result.kind === "ok") {        self.setLoading(false)        self.setCurrentUser(result.currentUser)        rootStore.errorStore.setErrors([] as Error[])      } else {        self.setLoading(false)        __DEV__ && console.tron.log("error", result)        console.log("error", result)        const errors = result?.messages?.map((message) => {          return {            message,          } as Error        })        rootStore.errorStore.setErrors(errors || ([{ message: result.kind }] as Error[]))      }    },
  React.useEffect(() => {    async function fetchData() {      await authStore.loadCurrentUser()    }    fetchData()  }, [])

I need help guys who can help me ?


Viewing all articles
Browse latest Browse all 6287

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>