Using async and await with export const











up vote
3
down vote

favorite












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 :)



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 is this:



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


it's return a promise










share|improve this question






















  • Change it from an 'export const' (ie. use 'let'); is that relevant to the problem/issue? If not, remove it from the title. Remember to eliminate non-relevant information and reduce problem scope.
    – user2864740
    Jun 11 at 18:47








  • 1




    return dispatch => {...} needs to also be async I believe. Right now, only the top level function is async, not the nested one.
    – zero298
    Jun 11 at 18:47

















up vote
3
down vote

favorite












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 :)



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 is this:



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


it's return a promise










share|improve this question






















  • Change it from an 'export const' (ie. use 'let'); is that relevant to the problem/issue? If not, remove it from the title. Remember to eliminate non-relevant information and reduce problem scope.
    – user2864740
    Jun 11 at 18:47








  • 1




    return dispatch => {...} needs to also be async I believe. Right now, only the top level function is async, not the nested one.
    – zero298
    Jun 11 at 18:47















up vote
3
down vote

favorite









up vote
3
down vote

favorite











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 :)



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 is this:



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


it's return a promise










share|improve this question













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 :)



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 is this:



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


it's return a promise







javascript reactjs react-native






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jun 11 at 18:45









Marco Jr

1,66872347




1,66872347












  • Change it from an 'export const' (ie. use 'let'); is that relevant to the problem/issue? If not, remove it from the title. Remember to eliminate non-relevant information and reduce problem scope.
    – user2864740
    Jun 11 at 18:47








  • 1




    return dispatch => {...} needs to also be async I believe. Right now, only the top level function is async, not the nested one.
    – zero298
    Jun 11 at 18:47




















  • Change it from an 'export const' (ie. use 'let'); is that relevant to the problem/issue? If not, remove it from the title. Remember to eliminate non-relevant information and reduce problem scope.
    – user2864740
    Jun 11 at 18:47








  • 1




    return dispatch => {...} needs to also be async I believe. Right now, only the top level function is async, not the nested one.
    – zero298
    Jun 11 at 18:47


















Change it from an 'export const' (ie. use 'let'); is that relevant to the problem/issue? If not, remove it from the title. Remember to eliminate non-relevant information and reduce problem scope.
– user2864740
Jun 11 at 18:47






Change it from an 'export const' (ie. use 'let'); is that relevant to the problem/issue? If not, remove it from the title. Remember to eliminate non-relevant information and reduce problem scope.
– user2864740
Jun 11 at 18:47






1




1




return dispatch => {...} needs to also be async I believe. Right now, only the top level function is async, not the nested one.
– zero298
Jun 11 at 18:47






return dispatch => {...} needs to also be async I believe. Right now, only the top level function is async, not the nested one.
– zero298
Jun 11 at 18:47














3 Answers
3






active

oldest

votes

















up vote
5
down vote



accepted










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)
})
}
}
}





share|improve this answer





















  • What's the downvote here about?!
    – T.J. Crowder
    Jun 11 at 18:50










  • Thanx, Zero ! Yes, you are right ! that's the reason !
    – Marco Jr
    Jun 11 at 19:08


















up vote
1
down vote













It looks like it's because the function you return (dispatch => {...}) is not an async function, so you can't use await in it. You would need to do something like return async dispatch => {...}






share|improve this answer





















  • Exactly -- just because that inner function is inside an async function doesn't mean you can use await in it. The function await is used in, itself, must be async.
    – T.J. Crowder
    Jun 11 at 18:50










  • What's the downvote here about?!
    – T.J. Crowder
    Jun 11 at 18:51


















up vote
0
down vote













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();





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f50804207%2fusing-async-and-await-with-export-const%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    5
    down vote



    accepted










    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)
    })
    }
    }
    }





    share|improve this answer





















    • What's the downvote here about?!
      – T.J. Crowder
      Jun 11 at 18:50










    • Thanx, Zero ! Yes, you are right ! that's the reason !
      – Marco Jr
      Jun 11 at 19:08















    up vote
    5
    down vote



    accepted










    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)
    })
    }
    }
    }





    share|improve this answer





















    • What's the downvote here about?!
      – T.J. Crowder
      Jun 11 at 18:50










    • Thanx, Zero ! Yes, you are right ! that's the reason !
      – Marco Jr
      Jun 11 at 19:08













    up vote
    5
    down vote



    accepted







    up vote
    5
    down vote



    accepted






    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)
    })
    }
    }
    }





    share|improve this answer












    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)
    })
    }
    }
    }






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Jun 11 at 18:49









    zero298

    11.3k32954




    11.3k32954












    • What's the downvote here about?!
      – T.J. Crowder
      Jun 11 at 18:50










    • Thanx, Zero ! Yes, you are right ! that's the reason !
      – Marco Jr
      Jun 11 at 19:08


















    • What's the downvote here about?!
      – T.J. Crowder
      Jun 11 at 18:50










    • Thanx, Zero ! Yes, you are right ! that's the reason !
      – Marco Jr
      Jun 11 at 19:08
















    What's the downvote here about?!
    – T.J. Crowder
    Jun 11 at 18:50




    What's the downvote here about?!
    – T.J. Crowder
    Jun 11 at 18:50












    Thanx, Zero ! Yes, you are right ! that's the reason !
    – Marco Jr
    Jun 11 at 19:08




    Thanx, Zero ! Yes, you are right ! that's the reason !
    – Marco Jr
    Jun 11 at 19:08












    up vote
    1
    down vote













    It looks like it's because the function you return (dispatch => {...}) is not an async function, so you can't use await in it. You would need to do something like return async dispatch => {...}






    share|improve this answer





















    • Exactly -- just because that inner function is inside an async function doesn't mean you can use await in it. The function await is used in, itself, must be async.
      – T.J. Crowder
      Jun 11 at 18:50










    • What's the downvote here about?!
      – T.J. Crowder
      Jun 11 at 18:51















    up vote
    1
    down vote













    It looks like it's because the function you return (dispatch => {...}) is not an async function, so you can't use await in it. You would need to do something like return async dispatch => {...}






    share|improve this answer





















    • Exactly -- just because that inner function is inside an async function doesn't mean you can use await in it. The function await is used in, itself, must be async.
      – T.J. Crowder
      Jun 11 at 18:50










    • What's the downvote here about?!
      – T.J. Crowder
      Jun 11 at 18:51













    up vote
    1
    down vote










    up vote
    1
    down vote









    It looks like it's because the function you return (dispatch => {...}) is not an async function, so you can't use await in it. You would need to do something like return async dispatch => {...}






    share|improve this answer












    It looks like it's because the function you return (dispatch => {...}) is not an async function, so you can't use await in it. You would need to do something like return async dispatch => {...}







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Jun 11 at 18:48









    Robert Herhold

    12912




    12912












    • Exactly -- just because that inner function is inside an async function doesn't mean you can use await in it. The function await is used in, itself, must be async.
      – T.J. Crowder
      Jun 11 at 18:50










    • What's the downvote here about?!
      – T.J. Crowder
      Jun 11 at 18:51


















    • Exactly -- just because that inner function is inside an async function doesn't mean you can use await in it. The function await is used in, itself, must be async.
      – T.J. Crowder
      Jun 11 at 18:50










    • What's the downvote here about?!
      – T.J. Crowder
      Jun 11 at 18:51
















    Exactly -- just because that inner function is inside an async function doesn't mean you can use await in it. The function await is used in, itself, must be async.
    – T.J. Crowder
    Jun 11 at 18:50




    Exactly -- just because that inner function is inside an async function doesn't mean you can use await in it. The function await is used in, itself, must be async.
    – T.J. Crowder
    Jun 11 at 18:50












    What's the downvote here about?!
    – T.J. Crowder
    Jun 11 at 18:51




    What's the downvote here about?!
    – T.J. Crowder
    Jun 11 at 18:51










    up vote
    0
    down vote













    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();





    share|improve this answer



























      up vote
      0
      down vote













      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();





      share|improve this answer

























        up vote
        0
        down vote










        up vote
        0
        down vote









        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();





        share|improve this answer














        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();






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 11 at 0:37

























        answered Nov 11 at 0:11









        Roman

        2,3281424




        2,3281424






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f50804207%2fusing-async-and-await-with-export-const%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            List item for chat from Array inside array React Native

            Thiostrepton

            Jo Brand