用回调函数通知或者使用 async function
function getData(callback) {
const data = []
console.info(data)
setTimeout(_ => {
data.push(+new Date())
typeof callback === 'function' && callback(data)
}, 1e3)
}
getData(data => console.info(data))
async function getData() {
const sleep = delay => new Promise(resolve => setTimeout(resolve, delay || 0))
const data = []
console.info(data)
await sleep(1000)
data.push(+new Date())
return data
}
;(async _ => {
console.info(await getData())
})()
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…