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
javascript reactjs react-native
add a comment |
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
javascript reactjs react-native
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 beasyncI believe. Right now, only the top level function is async, not the nested one.
– zero298
Jun 11 at 18:47
add a comment |
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
javascript reactjs react-native
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
javascript reactjs react-native
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 beasyncI believe. Right now, only the top level function is async, not the nested one.
– zero298
Jun 11 at 18:47
add a comment |
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 beasyncI 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
add a comment |
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)
})
}
}
}
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
add a comment |
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 => {...}
Exactly -- just because that inner function is inside anasyncfunction doesn't mean you can useawaitin it. The functionawaitis used in, itself, must beasync.
– T.J. Crowder
Jun 11 at 18:50
What's the downvote here about?!
– T.J. Crowder
Jun 11 at 18:51
add a comment |
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();
add a comment |
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)
})
}
}
}
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
add a comment |
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)
})
}
}
}
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
add a comment |
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)
})
}
}
}
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)
})
}
}
}
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
add a comment |
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
add a comment |
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 => {...}
Exactly -- just because that inner function is inside anasyncfunction doesn't mean you can useawaitin it. The functionawaitis used in, itself, must beasync.
– T.J. Crowder
Jun 11 at 18:50
What's the downvote here about?!
– T.J. Crowder
Jun 11 at 18:51
add a comment |
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 => {...}
Exactly -- just because that inner function is inside anasyncfunction doesn't mean you can useawaitin it. The functionawaitis used in, itself, must beasync.
– T.J. Crowder
Jun 11 at 18:50
What's the downvote here about?!
– T.J. Crowder
Jun 11 at 18:51
add a comment |
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 => {...}
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 => {...}
answered Jun 11 at 18:48
Robert Herhold
12912
12912
Exactly -- just because that inner function is inside anasyncfunction doesn't mean you can useawaitin it. The functionawaitis used in, itself, must beasync.
– T.J. Crowder
Jun 11 at 18:50
What's the downvote here about?!
– T.J. Crowder
Jun 11 at 18:51
add a comment |
Exactly -- just because that inner function is inside anasyncfunction doesn't mean you can useawaitin it. The functionawaitis used in, itself, must beasync.
– 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
add a comment |
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();
add a comment |
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();
add a comment |
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();
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();
edited Nov 11 at 0:37
answered Nov 11 at 0:11
Roman
2,3281424
2,3281424
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 beasyncI believe. Right now, only the top level function is async, not the nested one.– zero298
Jun 11 at 18:47