0

Function outside await : return error Can not use keyword await Help me please...


I18n.defaultLocale = 'tr'
await AsyncStorage.getItem('locale').then((value) => {
 I18n.locale = value        
}).done();


I18n.fallbacks = true;
I18n.translations = { en, tr };

const currentLocale = I18n.currentLocale();

export function strings(name, params = {}) {
  return I18n.t(name, params);
};

export default I18n;

2 Answers 2

1

You must do something like this with await/async :

var getItem = async function() {
  // await can be used here
  await AsyncStorage.getItem('locale').then((value) => {
    I18n.locale = value        
  }).done();
}

and call it like this :

getItem(); // sync

or

await getItem(); // in another async function

Edit : With async function you can "do the same things" than a none async function. It's just await that is requiring particular wrapping.

Sign up to request clarification or add additional context in comments.

2 Comments

thank you getItem(); call other files ? a.js - getitem function b.js - call a.js call getitem function ?
just export/import the function if you want reuse it in another file
0

An async function can contain an await expression that pauses the execution of the async function and waits for the passed Promise's resolution, and then resumes the async function's execution and returns the resolved value.

Remember, the await keyword is only valid inside async functions. If you use it outside of an async function's body, you will get a SyntaxError.

you don't need to use then when using async/await,

for more details see async function on MDN Web docs

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.