异步等待-等待会阻止其他代码运行吗? [英] Async await - does await block other code from running?

查看:123
本文介绍了异步等待-等待会阻止其他代码运行吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在javascript中,是否等待阻止代码?例如,假设我们有以下代码:

In javascript, does await block code? For example, let's say we have the below code:

async function queryDB() {
    const addUser = await promisePool.execute("INSERT INTO Users (User) VALUES ('username')")
    const selectUser = await promisePool.execute("SELECT User FROM Users") 
}

将"selectUser"等待运行直到addUser完成,以便我们可以选择添加的用户?

也可以说,我们在等待之间添加了一些不是承诺的代码,如下所示:

Also, let's say that we add some code between the awaits that is not a promise, something like this:

    async function queryDB() {
        const addUser = await promisePool.execute("INSERT INTO Users (User) VALUES ('username')")

setTimeout(() => console.log('Do something that takes 3 seconds'), 3000);

        const selectUser = await promisePool.execute("SELECT User FROM Users") 
    }

将"selectUser"等待addUser但不等待setTimeout吗?如果是这样,您将如何编写上面的代码以使addUser先运行,然后setTimeout然后再selectUser?

我还想补充一点,我一直在研究和阅读stackoverflow和其他资源,但是我需要澄清一下.

I would also like to add that I have been studying and reading on both stackoverflow and other resources, but I need some clarification.

推荐答案

将"selectUser"等待运行,直到addUser完成,以便我们可以选择要添加的用户?

Will "selectUser" wait to run until addUser is finished so that we can select the user that is added?

来自 MDN文档-等待:

await表达式导致异步函数执行暂停直到承诺得到解决(即实现或拒绝),然后继续实现后执行异步功能.恢复后,等待表达式的值就是已实现的Promise的值.

The await expression causes async function execution to pause until a Promise is settled (that is, fulfilled or rejected), and to resume execution of the async function after fulfillment. When resumed, the value of the await expression is that of the fulfilled Promise.

queryDB 函数的执行将在等待第一个承诺履行时被暂停.第二行代码仅在第一个代码成功完成之后执行.

Execution of queryDB function will be paused while it waits for the first promise to settle. Second line of code will only execute after first one has completed successfully.

将"selectUser"等待addUser但不等待setTimeout吗?

Will "selectUser" wait for addUser but not the setTimeout?

是的,这是正确的.

如果是这样,您将如何编写上面的代码以使addUser首先运行,然后setTimeout,然后选择用户?

If so, how would you write the above code to make addUser run first, then setTimeout and then selectUser?

您可以将 setTimeout 包装在一个返回诺言的函数中,然后等待该诺言以确保在前两个完成后执行最后一行.

You could wrap setTimeout in a function that returns a promise and then await that promise to make sure last line executes after first two have completed.

以下是包装 setTimeout

function waitForTimeout(seconds) {
  return new Promise((resolve, reject) => {
     setTimeout(() => { 
        console.log("Hello World");
        resolve();
     }, seconds * 1000);        
  });
}

一旦有了包装器功能,您就可以等待,如下所示:

Once you have a wrapper function, you can await it as shown below:

async function queryDB() {
    const addUser = await promisePool.execute(
        "INSERT INTO Users (User) VALUES ('username')"
    );

    await waitForTimeout(3);    // wait for 3 seconds

    const selectUser = await promisePool.execute("SELECT User FROM Users") 
}

这篇关于异步等待-等待会阻止其他代码运行吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆