快速搞定前端JS面试 -- 第五章 JS基础-异步(同步异步、应用场景、回调地狱)

第五章 JS基础-异步

一、单线程和异步

二、应用场景

三、callback hell回调地狱

四、问题解答

1. 同步和异步的区别是什么?

2. 手写promise加载一张图片?

3. 前端使用异步的场景?

4. 场景题setTimeOut


题目

  1. 同步和异步的区别是什么?
  2. 手写promise加载一张图片?
  3. 前端使用异步的场景?
  4. 场景题setTimeOut

知识点

  1. 单线程和异步
  2. 应用场景
  3. Callback hell 和promise

一、单线程和异步

  • JS是单线程语言,只能同时做一件事
  • 浏览器和nodejs已经支持JS启动进程,如web worker
  • JS和DOM渲染共用同一个线程,因为JS可修改DOM结构
  • 遇到等待(网络请求,定时任务)不能卡住
  • 需要异步
  • 回调callback函数形式
// 异步 (callback 回调函数)
console.log(100)
setTimeout(() => {
    console.log(200)
}, 1000)
console.log(300)
console.log(400)
// 结果100 300 400 200

// 同步
console.log(100)
alert(200)
console.log(300)
// 结果 100 200 300
// 结果 100 弹框200点击之后才会出现 300(卡住)

先执行同步任务,到一定时间或者要求再执行回调函数

异步不会阻塞后面代码的执行,但是同步会

二、应用场景

    1. 网络请求,如Ajax图片加载

// ajax
console.log('start')
$get('./data1.json',function(data1){   // 回调函数网络请求
    console.log(data1)
})
console.log('end')

// 图片加载
console.log('start')
let img = document.createElement('img')
img.onload = function () {    // callback
    console.log('onload')
}
img.src = 'xx.png'
console.log('end')

    2. 定时任务,如setTimeout

// setTimeout
console.log(100)
setTimeout(function () {
    console.log(200)
}, 1000)
console.log(300)

// setInterval
console.log(100)
setInterval(function() {
    console.log(200)
}, 1000)
console.log(300)

三、callback hell回调地狱

是一种越陷越深的嵌套形式

//callback hell
// 获取第一份数据
$get(url1, (data1) => {
    console.log(data1)
    // 获取第二份数据
    $get(url2, (data2) => {
        console.log(data2)
        // 获取第三份数据
        $get(url3, (data3) => {
            console.log(data3)
            // 获取更多数据
        })
    })
})

解决方法:使用promise

// promise
function getData(url) {
    return new Promise((resolve, reject) => {
        $.ajax({
            url,
            success(data) {
                resolve(data)
            },
            error(err) {
                reject(err)
            }
        })
    })
}

const url1 = 'data1.json'
const url2 = 'data2.json'
const url3 = 'data3.json'
getData(url1).then(data1 => {
    console.log(data1)
    return getData(url2)
}).then(data2 => {
    console.log(data2)
    return getData(url3)
}).then(data3 => {
    console.log(data3)
}).catch(er => console.error(err))    // 串联形式

四、问题解答

1. 同步和异步的区别是什么?

异步是基于JS单线程语言

异步不会阻塞代码执行

同步会阻塞代码执行

2. 手写promise加载一张图片?

function loadImg(src) {
    const p = new Promise(  
        (resolve, reject) => {   // resolve和reject也是函数
            const img = document.createElement('img')   // 定义图片
            img.onload = () => {    
                resolve(img)
            }
            img.onerror = () => {
                const err = new Error(`图片加载失败 ${src}`)
                reject(err)
            }
            img.src = src
        }
    )
    return p
}

// const url = 'https://img.mukewang.com/5a9fc8070001a82402060220-140-140.jpg'
// loadImg(url).then(img => {
//     console.log(img.width)
//     return img    // img传入then里
// }).then(img => {
//     console.log(img.height)
// }).catch(ex => console.error(ex))
const url1 = 'https://img.mukewang.com/5a9fc8070001a82402060220-140-140.jpg'
const url2 = 'https://img3.mukewang.com/5a9fc8070001a82402060220-100-100.jpg'
loadImg(url1).then(img1 => {
    console.log(img1.width)
    return img1 // return普通对象,then直接接收
}).then(img1 => {
    console.log(img1.height)
    return loadImg(url2) // promise 实例,则then之后接收到的是loadImg()执行完的img对象
}).then(img2 => {
    console.log(img2.width)
    return img2
}).then(img2 => {
    console.log(img2.height)
}).catch(ex => console.error(ex))

3. 前端使用异步的场景?

     网络请求,如Ajax图片加载

     定时任务,如setTimeout

4. 场景题setTimeOut

// setTimeou笔试题
console.log(1)
setTimeout(function() {
    console.log(2)
}, 1000)
console.log(3)
setTimeout(function() {
    console.log(4)
}, 10)
console.log(5)
// 结果1 3 5 4 2

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小白Rachel

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值