用 async/await 来处理异步实例

 

 

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>用 async/await 来处理异步实例</title>
<style type="text/css">
</style>
</head>
<body>
<script type="text/javascript">
function doubleAfter2seconds(num) {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve(2 * num)
        }, 2000);
    } )
}
async function timeout() {
    var result = await doubleAfter2seconds(30);
    return result;
}
timeout().then(result => {
    console.log(result);
})
console.log('虽然在后面,但是我先执行');
</script>
</body>
</html>

 

posted @ 2018-08-01 16:06  徐同保  阅读(292)  评论(0编辑  收藏  举报