当前位置: 首页 > news >正文

百度网站建设北京网络营销平台有哪些

百度网站建设北京,网络营销平台有哪些,wordpress文章分类链接,做诈骗网站以及维护1. 什么是 async/await async/await 是 ES8(ECMAScript 2017)引入的新语法,用来简化 Promise 异步操作。在 async/await 出 现之前,开发者只能通过链式 .then() 的方式处理 Promise 异步操作。示例代码如下: import …

1. 什么是 async/await

async/await ES8(ECMAScript 2017)引入的新语法,用来简化 Promise 异步操作。在 async/await 出 现之前,开发者只能通过链式 .then() 的方式处理 Promise 异步操作。示例代码如下:

import thenFs from 'then-fs'thenFs.readFile("./files/1.txt",'utf8') // 返回值是 promise 的实例对象。
.then(r1=>{   // 通过 .then 为第一个 Promise 实例指定成功之后的回调函数。console.log(r1)return thenFs.readFile("./files/2.txt",'utf8')  // 在第一个 .then 中返回一个新的 promise 的实例对象。
})
.then(r2=>{ // 继续调用 .then 为上一个 .then 的返回值(新的 Promise 实例) 指定成功之后的回调函数。console.log(r2)return thenFs.readFile("./files/3.txt",'utf8')  // 在第二个 .then 中返回一个新的 promise 的实例对象。
})
.then(r3=>{ // 继续调用 .then 为上一个 .then 的返回值(新的 Promise 实例) 指定成功之后的回调函数。console.log(r3)
})// 运行结果:
// txt file 1
// txt file 2
// txt file 3
  • .then 链式调用的优点: 解决了回调地狱的问题
  • .then 链式调用的缺点: 代码冗余、阅读性差、 不易理解 

 2. async/await 的基本使用

import thenFs from "then-fs";//按照顺序读取 1,2,3文件的内容
async function getAllFile(){const r1 = await thenFs.readFile("./files/1.txt",'utf8')// 当在 thenFs.readFile()方法前面添加 await 关键字时,返回的不是一个 Promise 实例了,而是文件的内容。console.log(r1)const r2 = await thenFs.readFile("./files/2.txt",'utf8')console.log(r2)const r3 = await thenFs.readFile("./files/3.txt",'utf8')console.log(r3)
}
//调用方法
getAllFile();// 运行结果:
// txt file 1
// txt file 2
// txt file 3

3. async/await 的使用注意事项

  1. 如果在 function 中使用了 await,则 function 必须被 async 修饰
  2. 在 async 方法中,第一个 await 之前的代码会同步执行,await 之后的代码会异步执行
import thenFs from "then-fs";console.log("A")
//按照顺序读取 1,2,3文件的内容
async function getAllFile(){console.log("B")const r1 = await thenFs.readFile("./files/1.txt",'utf8')const r2 = await thenFs.readFile("./files/2.txt",'utf8')const r3 = await thenFs.readFile("./files/3.txt",'utf8')console.log(r1,r2,r3)console.log("D")
}//调用方法
getAllFile();
console.log("C")// 运行结果:
// A
// B
// C
// txt file 1
// txt file 2
// txt file 3
// D
http://www.ds6.com.cn/news/79485.html

相关文章:

  • 网站建设公司重庆成都网站建设企业
  • 做时时彩开奖网站犯法吗网站外链怎么发布
  • 南京做网站牛杭州网站免费制作
  • 做网站能成功吗点击宝seo
  • wordpress适合做商城吗百度快速排名优化技术
  • 网页设计师网站网络营销公司注册找哪家
  • 做网站需要什么技术支持沈阳关键词优化价格
  • 零基础搭建网站火狐搜索引擎
  • 信誉好的宜昌网站建设制作一个网页的步骤
  • 单位网站建设注意事项今日热搜榜
  • 邢台移动端网站建设网站seo优化方案
  • 建设企业网站电话是多少集团网站推广
  • 广州网站备案公司天津seo托管
  • 设计师的个人网站网站推广排名哪家公司好
  • 建网站规则小程序开发需要多少钱
  • 网站开发前端兼职上百度推广的网站要多少钱
  • 做水果的b2b网站商丘seo教程
  • 外贸网站制作有哪些seo查询百科
  • 餐饮加盟网站制作关键词的作用
  • 房山广州网站建设seo还有前景吗
  • 锦州网站建设怎样个人免费推广网站
  • 学做ps的网站网站怎么优化搜索
  • 在运行 打开wordpress网站seo查询工具
  • 网站seo 工具google站长工具
  • 鲜花网站数据库建设重庆森林电影简介
  • 多个织梦dedecms网站怎么做站群搜索引擎yandex入口
  • 网页设置背景图片武汉seo优化代理
  • 网站ip域名查询百度识别图片找图
  • 东莞工业设计公司优质的seo快速排名优化
  • 成都专业建网站公司国外seo