功能外部等待:返回错误无法使用关键字等待请帮我...
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;
您必须使用等待/异步进行这样的事情:
var getItem = async function() {
// await can be used here
await AsyncStorage.getItem('locale').then((value) => {
I18n.locale = value
}).done();
}
这样称呼:
getItem(); // sync
或
await getItem(); // in another async function
编辑:使用异步函数,您可以"做相同的事情",而不是一个异步函数。只是await
需要特定包装。
async
函数可以包含一个 await
表达式,该表达式暂停async
函数的执行并等待传递的Promise的分辨率,然后恢复异步函数的执行并返回已解决的值。blockquote>
请记住,await
关键字仅在async
功能中有效。 如果您在async
函数的身体外使用它,您将得到一个 syntaxerror。
使用async/await
,
then
有关更多详细信息,请参见MDN Web Docs上的异步功能