整理下之前做的有点疑惑的js基础题目和大家分享以下,如果大家觉得有用,别忘了点一下赞哦
async
async function t1(){
//flag1
let a = await "小刘"
console.log(a)
}
async function t2(){
let a = await new Promise(resolve =>{
})
console.log(a)
}
async function t3(){
let a = await new Promise(resolve=>{
resolve()})
console.log(a)
}
async function t4(){
let a = await new Promise(resolve=>{
resolve("hello")})
console.log(a)
}
async function t5(){
let a = await new Promise(resolve=>{
resolve("hello")})
.then(()=>{
return "xiaoniu"})
console.log(a)
}
async function t6(){
//flag6
let a = fn().then(res=>{
return res})
console.log(a)
}
async function fn(){
await new Promise(resolve=>{
resolve("joke")})
}
t1() // Promise { <pending> }
t2() // 小刘
t3() // undefined
t4() // hello
t5() // xiaoniu
t6() // Promise {<resolved>: undefined}
flag1处:await一个非promise对象,会返回Promise { <pending> }
对象
flag6处:fn() 返回了一个promise<padding>
对象 ,await 返回promse成功的回调,而这个 promise 并没有回调,所以promise状态变为了 <resolved>
,而返回值时undefined
。
这题我也没理解透彻