const obj = {
name:'小花’,
age:25,
city:‘哈尔滨’
}
console.log( Object.keys(obj) ) //[‘小花’,‘25’,‘哈尔滨’]
console.log( Object.value(obj) ) //[‘name’,‘age’,‘city’]
console.log( Object.entries(obj) )
//[Array(2), Array(2), Array(2)] =>
//0: (2) [“name”, “小花”]
//1: (2) [“age”, 25]
//2: (2) [“city”, “哈尔滨”]
//length: 3
Object.entries(obj).map( ([key,value]) => {
console.log(key,value)
})
// name 小花
// age 25
// city 哈尔滨
Promise
const url = ‘https://blogs.zdldove.top/Home/Apis/listWithPage’
const p = new Promise((resolve, reject) => {
resolve(‘小花’)
})
p
.then( res => {
return axios.post(url)
})
.then( res => {
return axios.post(url)
})
.catch( err => {
})
/*
以上现请求一个等成功之后请求另一个
*/
function one () {
return axios.post(url)
}
function two () {
return axios.post(url)
}
const num = Promise.all([one(), two() ])
num
.then( res => {
console.log(res, 1)
})
/*
等待两个全都请求完毕 之后一起返回
*/
根据e6 promise 也可以写成 =》
promise.resolve(‘小花’)
.then(res) => {
console.log(res)
}
promise 的常用四种用法
promise.resolve //成功
promise.reject //失败
promise.all //等待多个异步处理一起返回
promise.race // 比赛 谁先完事 就是谁的状态
async await
const p1 = () => axios.post(url)
const p2 = () => axios.post(url)
async function func() {
const d1 = await p1() //异步写成同步的样子
const d2 = await p2() //先执行上面的请求,请求完毕之后再执行下面的请求
console.log(d1,d2)
}
func()
const p1 = () => axios.post(url)
const p2 = () => axios.post(url)
const p3 = () => axios.post(url)
async function func() {
const x0 = p1()
const x1 = p2()
const d1 = await x0
const d2 = await x1
const d3 = await p3()
return [d1,d2,d3]
}
func()
.then(res =>{
console.log(res,1)
})
//用promise.all方法 也可以 代码简便
async function func() {
const d0 = await promise.all([p1(),p2()])
const d3 = await p3()
return [d0,d3]
}
func()
.then(res =>{
console.log(res,1)
})