基于业务的promise + async/await的简易demo
netRequest()
是网络请求函数
init()
是页面初始化函数
handler()
是某个业务函数,例如按钮触发的handle函数。
const netRequest = async () => {
console.log("netRequest函数")
const resp = await myAxios()
console.log(resp)
}
const init = () => {
console.log("init函数")
}
const myAxios = () => {
return new Promise(((resolve, reject) => {
setTimeout(() => {
resolve("这是data")
}, 3000)
}))
}
const handler = () => {
netRequest().then(
() => {
init()
}
)
}
handler()
// 执行结果
netRequest函数
这是data
init函数