回调函数
- 回调函数 本质上就是一个普通函数
- 一个函数A以实参的形式传递到函数B中
- 在函数B中,以形参的方式调用函数A
- 此时 函数A就可以称为 函数B的回调函数
- 回调函数的使用场景为异步代码的解决方案
function fn(chenggong, shibai) {
const timer = Math.ceil(Math.random() * 3000)
console.log('帮忙去买瓶水')
setTimeout(() => {
if(timer > 2500) {
console.log('买水成功,用时', timer)
shibai()
} else {
console.log('买水成功,用时', timer)
chenggong()
}
}, timer)
}
fn(
() => {console.log('开玩笑的,退了吧')},
() => {console.log('买不到就别回来了')}
)
回调地狱
- 这不是我们写代码的时候出现的某个漏洞
- 只是我们在利用回调函数解决问题的时候,代码量多了之后的一个视觉体验
- 回调地狱的代码不利于我们去维护或者管理
- 此时出现了一个东西叫做promise,也是一个异步代码的解决方案
function fn(chenggong, shibai) {
const timer = Math.ceil(Math.random() * 3000)
console.log('让A帮忙去买瓶水')
setTimeout(() => {
if(timer > 200) {
console.log('买水成功,用时', timer)
shibai()
} else {
console.log('买水成功,用时', timer)
chenggong()
}
}, timer)
}
fn(
() => {
fn(
() => {console.log('A买完水后,又买了一箱饮料')},
() => {console.log('A就买了一瓶水,他不愿意给你们买饮料')}
)
},
() => {
fn(
() => {console.log('A第二次买水 成功了')},
() => {
fn(
() => {console.log('A第二次买水 成功了')},
() => {console.log('A第二次买水 失败了')}
)
}
)
}
)