admin管理员组文章数量:1333054
So I'm trying to wrap my head about promises/await/async. I can't understand why when go() is executed the alert with "finished" go right after the console.log(coffee). Why does it only wait for getCoffee() and the other axios calls are ran after the "finished" alert when all functions are using await/promises?
function getCoffee() {
return new Promise(resolve => {
setTimeout(() => resolve("☕"), 2000); // it takes 2 seconds to make coffee
});
}
async function go() {
try {
alert("ok");
const coffee = await getCoffee();
console.log(coffee); // ☕
const wes = await axios("/?results=200");
console.log("wes"); // using string instead of value for brevity
const wordPromise = axios("/?results=200");
console.log("wordPromise"); // using string instead of value for brevity
alert("finish");
} catch (e) {
console.error(e); //
本文标签:
Javascript awaitasync order of executionStack Overflow
版权声明:本文标题:Javascript awaitasync order of execution - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人,
转载请联系作者并注明出处:http://www.betaflare.com/web/1742247163a2440066.html,
本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论