async、await、promise用法举例

例子1:
function one() {
return 'i am one';
}
function two() {
setTimeout(() => {
return 'i am two';
}, 1000);

}

function three() {
return 'i am three';
}

function run() {
console.log(one());
console.log(two());
console.log(three());
}

run();
输出结果
i am one
undefined
i am three
缺点:对于异步操作,无法同步输出结果的缺陷
例子2:
function one() {
return 'i am one';
}

function two() {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve('i am two')
}, 3000)
})

}

function three() {
return 'i am three';
}

async function run() {
console.log(one());
console.log(await two());
console.log(three());
}

run();
输出结果
i am one
i am two
i am three
功能:能够实现将异步变为同步的过程

全部评论

相关推荐

08-12 09:16
Java
牛客38753147...:后端的竞争者一届比一届卷,前两年非985还很多,一段大厂实习就已经非常优秀了。 现在985硕多如狗,人手一段大厂实习,而且腾讯和百度今年都宣布实习扩招了一倍不止,越来越多的人从本一研一就开始刷实习,信息差也基本没有了。可以预见的,以后只会越来越卷。
投递快手等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务