回调函数
function getSchool(callback) {
setTimeout(function () {
var age = 18;
if (Math.random() < 0.5) {
callback(age)
}
},5000)
}
getSchool(function (data) {
console.log(data)
})
promise方法
function getData(resolve,reject) {
setTimeout(function () {
var age = 19;
if (Math.random() < 0.5) {
resolve(age);
}else {
reject('吧G雅鲁')
}
})
}
const p1 = new Promise(getData);
p1.then(function (data) {
console.log(data);
})