RN踩坑‘await‘ expressions are only allowed within async functions and at the top levels of modules.

2023-05-16

以前很少在循环中执行ajax,今天突然遇到个场景用到。习惯使用map来遍历了,结果在map中遍历无法使用await方法。一时有点懵!!!后来发现普通的for循环中可以遍历。虽然不清楚为什么,但是问题总算是解决了。

错误信息

'await' expressions are only allowed within async functions and at the top levels of modules.

错误代码

this.state.citys.map((item)=>{
      await this.loadWetherInfo(item.location);
 });

正确写法

直接使用for循环遍历就可以了。

const {citys} = this.state;
for (let i = 0; i < citys.length; i++) {
  await this.loadWetherInfo(citys[i].location);
}

总结

在for循环中可以使用await,但是在map遍历中不可以使用await。这个小坑竟然浪费了我2个小时😭。
看来需要强化一下Promise的学习了。

https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Promise

参考

https://stackoverflow.com/questions/40140149/use-async-await-with-array-map

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

RN踩坑‘await‘ expressions are only allowed within async functions and at the top levels of modules. 的相关文章

随机推荐

  • vim中React JSX语法高亮插件vim-jsx-pretty

    项目地址 https github com MaxMEllon vim jsx pretty 安装方法 vim plug为例 xff0c vimrc加入 Plug span class token string 39 yuezk vim j
  • Error: ENOSPC: System limit for number of file watchers reached

    现象 RN开发使用expo创建项目遇到如下错误 Logs for your project will appear below Press Ctrl 43 C to exit node 18489 DEP0066 DeprecationWa
  • vmware中CentOS7使用NAT模式主机无法通过ip连接虚拟机(记录)

    背景 有客户遇到了个问题我帮其远程解决 xff0c 我把大概的解决思路记录一下 客户先遇到了个问题 xff1a vmware中CentOS7因为使用NAT模式无法上网 xff0c 所以一直使用桥接模式上网 使用桥接的模式虽然可以上网但是虚拟
  • Windows下oracle client安装exp导出imp导入表

    下载Windows 32位客户端 下载页面 xff1a https www oracle com cn database enterprise edition downloads oracle db11g win32 html 下载完成以后
  • kali新手入门教学(1)--安装后的配置

    第一次写博客 xff0c 可能经验不是很足 xff0c 有不到位的地方还请各位见谅 最近在自学kali 然后kali是一个更新较快的系统 xff0c 虽然网上教程也很多 xff0c 但有的可能过时了 xff0c 所以我就把我这段时间的经验分
  • CentOS7安装mysql8笔记

    全新安装MySQL的步骤 以下说明假定您尚未使用第三方分发的RPM软件包将MySQL安装在您的系统上 xff1b 实操记录 我仅在CentOS Linux release 7 9 2009 Core 下进行测试 xff0c 其他环境仅供参考
  • mysql数据库安装教程合集

    CentOS7安装mysql8笔记 https blog csdn net lxyoucan article details 116854446 archlinux使用docker运行mysql实例 https blog csdn net
  • CentOS7使用docker跑mysql8笔记

    什么是docker Docker 是一个开放源代码软件 xff0c 是一个开放平台 xff0c 用于开发应用 交付 xff08 shipping xff09 应用 运行应用 Docker允许用户将基础设施 xff08 Infrastruct
  • x509: certificate has expired or is not yet valid.

    CentOS7中使用docker运行报错如下 xff1a root 64 localhost sudo docker run hello world Unable to find image hello world latest local
  • archlinux配置Xfce+fcitx5中文输入法

    因为图形界面我用的相对比较少 xff0c 所以喜欢安装小巧一些的桌面管理器 Centos中我比较喜欢使用openbox xff0c 但在是archlinux不知道在openbox使用中文输入法 xff0c 搞了半天也没解决 xff0c 不想
  • vim自动关闭标签

    一行代码搞定 inoremap span class token operator lt span buffer span class token operator gt span span class token operator lt
  • vim中jsx语法高亮

    没安装高亮插件前 安装插件vim jsx pretty xff08 卡顿 xff09 安装插件 Plug span class token string 39 yuezk vim js 39 span Plug span class tok
  • vmware安装alpine linux

    alpinelinux之前都没听过 xff0c 后来因为termux和docker发现这个发布版本 对这个发行布的最大印象就是小 为什么要在vmware中安装alpine linux xff0c 因为它小哇 xff01 主要好奇它安装后到底
  • alpine安装oh my zsh

    今天在vmware中安装了alpine 第一件事情就想安装oh my zsh 毕竟用了就回不去的东西 vmware安装alpine linux https blog csdn net lxyoucan article details 117
  • alpine linux配置中文语言环境

    alpine默认是没有语言环境的 没有中文不舒服不说 有时可能还会遇到中文乱码 那就来尝试一下安装中文语言环境吧 xff01 检查语言环境 查看当前语言环境 span class token keyword echo span span c
  • kali新手入门教学(11)--ddos攻击

    DDOS 全名是 Distribution Denial of service 分布式拒绝服务攻击 xff0c 指借助于客户 服务器技术 xff0c 将多个计算机联合起来作为攻击平台 xff0c 对一个或多个目标发动 DoS 攻击 xff0
  • alpinelinux使用国内源

    alpinelinux默认的源实在太慢了 xff0c 那就切换成国内的镜像吧 xff01 我个人比较喜欢中科大的源 xff0c 快 xff0c 稳 xff01 中科大源 一般情况下 xff0c 将 etc apk repositories
  • alpine安装图形界面Xfce

    alpine安装图形界面Xfce xff0c 这个官方文档很细 xff1a https wiki alpinelinux org wiki Xfce Setup Prerequisites 我只是做个记录 xff0c 做个笔记好日后观看 s
  • alpine中使用linuxbrew会怎么样?

    之前我写的文章中已经提到了 xff0c 使用apk安装的软件包都没有中文 如果我在alpine中使用brew安装软件是不是就变相的解决了中文软件包的问题了呢 xff1f 但是使用alpine本身就是因为它小巧 xff0c 这样一来就忘记了初
  • RN踩坑‘await‘ expressions are only allowed within async functions and at the top levels of modules.

    以前很少在循环中执行ajax xff0c 今天突然遇到个场景用到 习惯使用map来遍历了 xff0c 结果在map中遍历无法使用await方法 一时有点懵 xff01 xff01 xff01 后来发现普通的for循环中可以遍历 虽然不清楚为