当前位置:网站首页>async / await
async / await
2022-07-06 23:42:00 【InfoQ】
async / await
//fn 是一个异步函数
async function fn() {
// await 关键字, 这个函数必须要有 async
const res = await pAjax({ url: './server/a.php', dataType: 'json' })
// 当 pAjax 发送的请求没有回来之前, res 不会被赋值
// 只有请求回来以后, res 才会被赋值
// 如果这段打印先于请求回来执行, res 没有结果
// 如果 res 有结果, 证明: 这段代码被延后执行了, 延后到后面的 promise 对象完成以后
console.log(res)
console.log('后续代码')
}
fn()
async function fn() {
const res1 = await pAjax({ url: './server/a.php', dataType: 'json' })
console.log('需求1: ', res1)
const res2 = await pAjax({ url: './server/b.php', dataType: 'json', data: res1 })
console.log('需求2: ', res2)
const res3 = await pAjax({ url: './server/c.php', dataType: 'json', data: res2 })
console.log('需求3: ', res3)
}
console.log('start')
fn()
console.log('end')
const div = document.querySelector('div')
div.addEventListener('click', async () => {
const res1 = await pAjax({ url: './server/a.php', dataType: 'json' })
console.log('需求1: ', res1)
const res2 = await pAjax({ url: './server/b.php', dataType: 'json', data: res1 })
console.log('需求2: ', res2)
const res3 = await pAjax({ url: './server/c.php', dataType: 'json', data: res2 })
console.log('需求3: ', res3)
})
generator 函数
// 当有了星号以后, fn 不再是一个函数了
function* fn() {
console.log('我是第一段 代码')
yield '第一段结束'
console.log('我是第二段 代码')
yield '第二段结束'
console.log('我是第三段 代码')
return '第三段结束'
}
// result 就是 fn 给生成一个 迭代器
const result = fn()
// 第一次, 从 fn 的开头执行到第一个 yield,
// 把 yield 后面的东西当作返回值
const first = result.next()
console.log(first)
// 第二次, 从第一次的 yield 后面开始执行到第二个 yield 结束
// 把 第二个 yield 后面的东西当作返回值
const second = result.next()
console.log(second)
const third = result.next()
console.log(third)
const arr = ['hello', 'world', '你好', '世界']
const obj = { name: 'jack' }
for (let key in arr) {
console.log(key, arr[key])
}
for (let value of arr) {
console.log(value)
}
const s = new Set(['hello', 'world', '你好', '世界'])
// 1. 添加
s.add(true)
const s = new Set(['hello', 'world', '你好', '世界'])
// 2. 删除
s.delete('世界')
const s = new Set(['hello', 'world', '你好', '世界'])
// 3. 判断
console.log(s.has('你好'))
// 6. for of 循环来遍历
for (let value of s) {
console.log(value)
}
去重
const arr = [1, 2, 3, 4, 5, 4, 3, 2, 3, 4, 5, 2, 1]
const res = [...new Set(arr)]
console.log(res)
边栏推荐
- Dj-zbs2 leakage relay
- App clear data source code tracking
- Educational Codeforces Round 22 B. The Golden Age
- Common skills and understanding of SQL optimization
- How can project managers counter attack with NPDP certificates? Look here
- Batch size setting skills
- 5阶多项式轨迹
- Mybaits之多表查询(联合查询、嵌套查询)
- Two person game based on bevy game engine and FPGA
- 5. 数据访问 - EntityFramework集成
猜你喜欢
Initial experience of annotation
Life experience of an update statement
数字化如何影响工作流程自动化
漏电继电器JOLX-GS62零序孔径Φ100
基于 hugging face 预训练模型的实体识别智能标注方案:生成doccano要求json格式
论文阅读【MM21 Pre-training for Video Understanding Challenge:Video Captioning with Pretraining Techniqu】
《4》 Form
Two person game based on bevy game engine and FPGA
拼多多商品详情接口、拼多多商品基本信息、拼多多商品属性接口
Preliminary practice of niuke.com (9)
随机推荐
导航栏根据路由变换颜色
English语法_名词 - 所有格
batch size设置技巧
How does redis implement multiple zones?
JD commodity details page API interface, JD commodity sales API interface, JD commodity list API interface, JD app details API interface, JD details API interface, JD SKU information interface
JVM (19) -- bytecode and class loading (4) -- talk about class loader again
How digitalization affects workflow automation
Getting started with DES encryption
Leakage relay llj-100fs
论文阅读【Semantic Tag Augmented XlanV Model for Video Captioning】
JHOK-ZBL1漏电继电器
Creation and use of thread pool
How Alibaba cloud's DPCA architecture works | popular science diagram
If you want to choose some departments to give priority to OKR, how should you choose pilot departments?
Egr-20uscm ground fault relay
1. AVL tree: left-right rotation -bite
Torch optimizer small parsing
Design, configuration and points for attention of network arbitrary source multicast (ASM) simulation using OPNET
项目经理如何凭借NPDP证书逆袭?看这里
Leetcode 1189 maximum number of "balloons" [map] the leetcode road of heroding