首页 > 代码库 > Async

Async

window.onload = function(){
document.getElementById(‘bottom‘).addEventListener(‘click‘, function(){
alert(11);
});
}

var sleep = function (time) {
return new Promise(function (resolve, reject) {
setTimeout(function () {
let data = http://www.mamicode.com/‘db‘;
resolve(data);
}, time);
})
};

var sleepAgain = function (time) {
return new Promise(function (resolve, reject) {
setTimeout(function () {
resolve();
}, time);
})
};

var start = async function () {
// 在这里使用起来就像同步代码那样直观
console.log(‘start‘);
let result = await sleep(3000);
console.log(result);
console.log(‘again‘);
await sleepAgain(3000);
console.log(‘end‘);
};

start();

Async