Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using async and await with export const

Tags:

I can't make this work...it's says: await is a reserved word. Yes, of course it is...and I'd like to use it :)

What's wrong ?

export const loginWithToken = async () => {   return dispatch => {     dispatch({type: SESSION_LOGIN_IN_PROGRESS, payload: true})     let storedData = await ReadFromLocalDB('user')     console.log(storedData)     if (!storedData) {         invalidToken(null, dispatch)     }     else {         storedData = JSON.parse(storedData)         SessionLoginWithToken(storedData.session.token).then(res => {             console.log(res)             loginSuccessfully(res, dispatch, true)         })     }   } } 

My ReadFromLocalDB function looks like this:

export const ReadFromLocalDB = async (key) => {    return AsyncStorage.getItem(key) } 

It returns a promise

like image 279
Marco Jr Avatar asked Jun 11 '18 18:06

Marco Jr


People also ask

How do I use async await in Const?

The syntax is as follow: you must declare a function to be async : const asyncFunction = async () => { // This is a good start } // or const asyncFunction = async function(){ // Old school function keyword? I like it! }

How do I export my Const?

Use named exports to export constants in JavaScript, e.g. export const A = 'a' and export const B = 'b' . The exported constants can be imported by using a named import as import {A, B} from './another-file. js' . You can have as many named exports as necessary in a file.

Can I use async await instead of promises?

Async/Await is used to work with promises in asynchronous functions. It is basically syntactic sugar for promises. It is just a wrapper to restyle code and make promises easier to read and use. It makes asynchronous code look more like synchronous/procedural code, which is easier to understand.

Does await wait for all promises?

If a Promise is passed to an await expression, it waits for the Promise to be fulfilled and returns the fulfilled value.


2 Answers

return dispatch => {...} needs to also be async I believe. Right now, only the top level function is async, not the nested one.

// This function is async export const loginWithToken = async () => {   // This one is not though which means it can't use await inside   // return dispatch => {    // Instead it should likely be:   return async dispatch => {     dispatch({type: SESSION_LOGIN_IN_PROGRESS, payload: true})     let storedData = await ReadFromLocalDB('user')     console.log(storedData)     if (!storedData) {         invalidToken(null, dispatch)     }     else {         storedData = JSON.parse(storedData)         SessionLoginWithToken(storedData.session.token).then(res => {             console.log(res)             loginSuccessfully(res, dispatch, true)         })     }   } } 
like image 163
zero298 Avatar answered Nov 27 '22 00:11

zero298


With export and import we are suggested to follow the model:

To define and export a function in the file myFile.js:

export const request = async (arg1, arg2) => {   try {     const response = await fetch('https://api.com/values/?arg1=' + arg1 + '&arg2=' arg2);     const json = await response.json();     console.log(json);   }   catch (e) {     console.log('We have the error', e);   } } 

To import and apply the function:

import {request} from './myFile'  request(arg1, arg2); 
like image 26
Roman Avatar answered Nov 26 '22 23:11

Roman