简体   繁体   English

解决承诺后,异步功能将不会返回

[英]Async function won't return when the promise is resolved

I have the following code: 我有以下代码:

let checkValue = async (nodeUrl, nodeName, value) => {

    let json = await getValue(nodeUrl, value);

    let obj = JSON.parse(json.value.information);

    for (let i = 0; i < obj.nodes.length; i++) {
        if (obj.nodes[i] === nodeName) {
            return Promise.resolve(true);
        }    
    }
    return Promise.resolve(false);
}

I tried to use Promise.resolve() to process the returned Promise value, but I still get Promise { <pending> } return. 我尝试使用Promise.resolve()处理返回的Promise值,但仍然得到Promise { <pending> }返回。 Why is this happening? 为什么会这样呢?

Let's explain a bit what happens in an async function. 让我们来解释一下async函数中会发生什么。

First off, all async functions return a promise. 首先,所有异步函数都返回一个Promise。 Always. 总是。 You will always need to use .then() or await to get the value from the returned promise. 您将始终需要使用.then()await从返回的await中获取值。

Second, Here's the sequence of events in your function. 其次,这是函数中事件的顺序。

  1. You call checkValue(...) and pass its arguments. 您调用checkValue(...)并传递其参数。
  2. The first line executes await getValue() . 第一行执行await getValue()
  3. This calls getValue() and receives a promise back. 这将调用getValue()并收到一个promise。
  4. Since you're doing an await , it suspends further execution of the function and, at that point in time, the the function returns an unresolved promise. 由于您正在执行await ,因此它将中止该函数的进一步执行,并且在那个时间点,该函数将返回一个未解决的Promise。
  5. That unresolved promise is what you are looking at. 您正在寻找那个无法解决的承诺。
  6. Meanwhile, the rest of your code after you called getValue() continues to execute. 同时,调用getValue()之后的其余代码将继续执行。
  7. Sometime later when the JS interpreter is done with what is was doing and goes to the event queue for the next event, it will find an event that leads to getValue() resolving its promise. 稍后某个时间,当JS解释器完成正在做的事情并转到下一个事件的事件队列时,它将找到一个导致getValue()解析其诺言的事件。
  8. At that point, the resolved value from the getValue() promise is assigned to your json variable and the execution of the checkValue() function is unpaused and continues. 那时,将getValue()承诺中的解析值分配给您的json变量,并且checkValue()函数的执行被取消暂停并继续执行。
  9. Then, depending upon the values in your for loop, your function will finish by returning a resolved promise. 然后,根据for循环中的值,函数将通过返回已解析的promise来完成。 Note, there is no reason to do return Promise.resolve(true) or return Promise.resolve(false) . 请注意,没有理由return Promise.resolve(true)return Promise.resolve(false) Just return true or return value is all that is required. 只需return truereturn value Whatever the return value is from an async function becomes the resolved value of the promise that was already returned from that function. 无论async函数的return值是什么,该值都会成为已从该函数returnasync的解析值。 The interpreter does that for you. 口译员为您完成。

I tried to use Promise.resolve() to process the returned Promise value, but I still get Promise { <pending> } return. 我尝试使用Promise.resolve()处理返回的Promise值,但仍然得到Promise { <pending> }返回。 Why is this happening? 为什么会这样呢?

First, you get a promise because all async functions return a promise. 首先,您将获得一个承诺,因为所有async函数都将返回一个承诺。

Second, it's unresolved at the time you examine it because the function was suspended at the first await and that's when the async function actually returned the promise and it is, at that point, unresolved because the function has not yet finished executing (due to the await ). 其次,它在您检查时尚未解决,因为该函数在第一次await时被挂起,并且那是async函数实际上返回了promise的那一刻,但由于该函数尚未完成执行而尚未解决(由于await )。 See step 4 above. 请参阅上面的步骤4。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM