【问题标题】:Getting the error 'await is a reserved word'收到错误“等待是保留字”
【发布时间】:2018-02-03 02:22:06
【问题描述】:

我需要从异步存储中加载一个 URL,然后我需要对其进行处理。

 async renderContent() {
    const url = await AsyncStorage.getItem(Keys.url );
    console.log("Got it url= "+url);
    return url;
},

它给了我错误await is a reserved word

更新: 现在它在没有 url 的情况下继续前进 Url got :(

【问题讨论】:

  • 您是否将 async 关键字添加到包含您的 await 语句的函数名称中?
  • @SwiftsNamesake :添加了适当的标签。 @Patrick Hund:添加了我的全部功能

标签: javascript react-native async-await


【解决方案1】:

asyncawait 是最新平台上的 ES2017 功能和支持。您应该检查您正在使用的平台。如果您的平台不支持最新的 ES2017 功能,那么您应该使用 Promises 之类的替代方法来进行异步操作。您可以通过以下链接查看支持http://caniuse.com/#search=await

AsyncStorage.getItem(Keys.url, function(err, result) {
    console.log(result)
})

【讨论】:

    【解决方案2】:

    在 ES6 环境中你将遵循模型:

    const request = async (arg1, arg2) => {
      const response = await fetch('https://api.com/values/?arg1=' + arg1 + '&arg2=' + arg2);
      const json = await response.json();
      console.log(json);
    }
    request();
    

    假设你的变量是之前定义的并应用匿名函数,我们应该有如下内容:

    async () => { 
      const url = await AsyncStorage.getItem(Keys.url );
      const urlJson = await url.json();
      console.log("Got it url= ", { urlJson });
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-02
      • 2018-12-25
      • 2021-11-15
      • 1970-01-01
      • 1970-01-01
      • 2021-11-05
      • 2023-03-12
      • 1970-01-01
      相关资源
      最近更新 更多