当前位置:网站首页>async / await
async / await
2022-07-07 05:39:00 【InfoQ】
async / await
//fn It's an asynchronous function
async function fn() {
// await keyword , This function must have async
const res = await pAjax({ url: './server/a.php', dataType: 'json' })
// When pAjax The request sent did not come back before , res Will not be assigned
// Only after asking to come back , res Will be assigned a value
// If this print comes back before the request , res No results
// If res It turns out , prove : This code has been delayed , Postpone to the later promise After the object is completed
console.log(res)
console.log(' Subsequent code ')
}
fn()
async function fn() {
const res1 = await pAjax({ url: './server/a.php', dataType: 'json' })
console.log(' demand 1: ', res1)
const res2 = await pAjax({ url: './server/b.php', dataType: 'json', data: res1 })
console.log(' demand 2: ', res2)
const res3 = await pAjax({ url: './server/c.php', dataType: 'json', data: res2 })
console.log(' demand 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(' demand 1: ', res1)
const res2 = await pAjax({ url: './server/b.php', dataType: 'json', data: res1 })
console.log(' demand 2: ', res2)
const res3 = await pAjax({ url: './server/c.php', dataType: 'json', data: res2 })
console.log(' demand 3: ', res3)
})
generator function
// When there is an asterisk , fn It's no longer a function
function* fn() {
console.log(' I'm the first paragraph Code ')
yield ' The first paragraph ends '
console.log(' I'm the second paragraph Code ')
yield ' The second paragraph ends '
console.log(' I'm the third paragraph Code ')
return ' The third paragraph ends '
}
// result Namely fn Generate a for iterator
const result = fn()
// for the first time , from fn Execute from the beginning of to the first yield,
// hold yield The latter is treated as a return value
const first = result.next()
console.log(first)
// The second time , From the first time yield Then start to execute to the second yield end
// hold the second yield The latter is treated as a return value
const second = result.next()
console.log(second)
const third = result.next()
console.log(third)
const arr = ['hello', 'world', ' Hello ', ' The 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', ' Hello ', ' The world '])
// 1. add to
s.add(true)
const s = new Set(['hello', 'world', ' Hello ', ' The world '])
// 2. Delete
s.delete(' The world ')
const s = new Set(['hello', 'world', ' Hello ', ' The world '])
// 3. Judge
console.log(s.has(' Hello '))
// 6. for of Loop to traverse
for (let value of s) {
console.log(value)
}
duplicate removal
const arr = [1, 2, 3, 4, 5, 4, 3, 2, 3, 4, 5, 2, 1]
const res = [...new Set(arr)]
console.log(res)
边栏推荐
- The navigation bar changes colors according to the route
- [JS component] custom select
- np. random. Shuffle and np Use swapaxis or transfer with caution
- 利用OPNET进行网络任意源组播(ASM)仿真的设计、配置及注意点
- 论文阅读【MM21 Pre-training for Video Understanding Challenge:Video Captioning with Pretraining Techniqu】
- Preliminary practice of niuke.com (9)
- Safe landing practice of software supply chain under salesforce containerized ISV scenario
- Is the human body sensor easy to use? How to use it? Which do you buy between aqara green rice and Xiaomi
- Jhok-zbg2 leakage relay
- Two person game based on bevy game engine and FPGA
猜你喜欢
阿里云的神龙架构是怎么工作的 | 科普图解
sql优化常用技巧及理解
Différenciation et introduction des services groupés, distribués et microservices
分布式全局ID生成方案
K6el-100 leakage relay
[paper reading] semi supervised left atrium segmentation with mutual consistency training
English语法_名词 - 所有格
How does mapbox switch markup languages?
Record a pressure measurement experience summary
Mysql database learning (8) -- MySQL content supplement
随机推荐
Jhok-zbg2 leakage relay
ssm框架的简单案例
Unity keeps the camera behind and above the player
《4》 Form
基于NCF的多模块协同实例
SAP webservice 测试出现404 Not found Service cannot be reached
np. random. Shuffle and np Use swapaxis or transfer with caution
Design, configuration and points for attention of network arbitrary source multicast (ASM) simulation using OPNET
How Alibaba cloud's DPCA architecture works | popular science diagram
消息队列:消息积压如何处理?
Record a pressure measurement experience summary
阿里云的神龙架构是怎么工作的 | 科普图解
论文阅读【MM21 Pre-training for Video Understanding Challenge:Video Captioning with Pretraining Techniqu】
分布式事务解决方案之TCC
nodejs获取客户端ip
【js组件】自定义select
WEB架构设计过程
“多模态”概念
Mysql database learning (8) -- MySQL content supplement
JVM(十九) -- 字节码与类的加载(四) -- 再谈类的加载器