await 暂停 等待 暂停的是什么

 

体验异步的终极解决方案-ES7的Async/Await

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

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

start();


体验异步的终极解决方案-ES7的Async/Await - CNode技术社区 https://cnodejs.org/topic/5640b80d3a6aa72c5e0030b6




对比


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

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

start();
console.log('AFTER start();');

 

 

 

function resolveAfter2Seconds() {
return new Promise(resolve => {
setTimeout(() => {
resolve('resolved');
}, 10000);
});
}

async function asyncCall() {
console.log('calling');
var result = await resolveAfter2Seconds();
console.log(result);
// expected output: 'resolved'
}

asyncCall();

 

 

 

 

 

 

 

 




posted @ 2018-08-31 02:53  papering  阅读(518)  评论(0编辑  收藏  举报