js是单线程的,在程序运行过程中,避免阻塞,那么异步执行就是不可缺少的。
在js中,常见的异步操作有哪些呢?ajax、定时器、Promise、async await等。
ajax之后再分享,先通过下面这段代码来体会一下其他这些异步操作
async function async1(){
console.log('async1 start')
await async2()
console.log('async1 end')
}
async function async2(){
console.log('async2')
}
console.log('script start')
setTimeout (function(){
console.log('setTimeout')
},0)
async1()
new Promise(function(resolve){
console.log('Promise1')
resolve()
}).then(function(){
console.log('Promise2')
})
console.log('script end')
代码运行的结果为:
script start
async1 start
async2
Promise1
script end
async1 end
Promise2
setTimeout
具体每一块之后再慢慢分享