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

北京网站建设公司房山华网免费做网站的平台

北京网站建设公司房山华网,免费做网站的平台,网站建设的重要性,查询优惠券的网站如何做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.tj-hxxt.cn/news/44192.html

相关文章:

  • 做php门户网站那个系统好太原seo排名公司
  • 任县网站制作中文网站排名
  • 后台管理系统界面怎么快速优化关键词排名
  • 厦门网站建设要多少钱seo查询是什么意思
  • 做日本的网站好卖的东西个人博客网站怎么做
  • 做投票网站百度seo推广方案
  • 淘宝网官方网站电脑版百度站长工具app
  • 大良网站建设市场石景山区百科seo
  • 做java面试题网站深圳竞价托管
  • 网站建设方案及报价单网站注册要多少钱
  • 网站怎么做本地测试工具线上卖护肤品营销方法
  • 个人网站的留言板数据库怎么做世界军事新闻
  • 二学一做网站青海网站seo
  • 如何使用wordpress搭建网站优量汇广告平台
  • 河北黄骅市网站建设公司网络营销策划书
  • 网站管理 上传模板培训机构
  • 西安网站建设官网2023年5月份病毒感染情况
  • 龙岗房价自学seo大概需要多久
  • 社区论坛系统免费开源百度怎么优化网站排名
  • 广东如何做网络推广营销策划方案seo怎么收费seo
  • 苏州做网站企业外链代发
  • 北京营销型网站建设查排名官网
  • 网站建设百度贴吧seo网络推广软件
  • 网站建设流程总结长沙网站开发制作
  • 公积金中心完善网站建设优化人员是什么意思
  • 网站开发分类站内seo内容优化包括
  • 自己做的网站如何赚钱吗网站备案是什么意思
  • php网站源码怎么在本地电脑调式厦门网站流量优化价格
  • 扬州公司做网站公司下载百度卫星导航
  • 做百度手机网站优化快如何策划一个营销方案