React Native Async等待呼叫返回错误



功能外部等待:返回错误无法使用关键字等待请帮我...


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上的异步功能

最新更新