简体   繁体   English

promise 内的多个 resolve() 调用返回异步 function

[英]Multiple resolve() calls inside promise returning async function

I have the following testAsync function that should return resolved promise after I call resolve() .我有以下 testAsync function 在我调用resolve()后应该返回已解决的 promise 。

async function wait(time) {
    await new Promise((resolve, reject) => { setTimeout(resolve, time) });
}

async function testAsync(testParam) {
    return new Promise(async (resolve, reject) => {
        try {
            await wait(2000); // do some async work
            if (testParam === 1) {
                console.log("test param is equal to 1");
                resolve("first IF");
            }
            if (testParam < 10) {
                console.log("test param is smaller than 10");
                resolve("second IF");
            }
        } catch (error) {
            reject(error);
        }
    });
}

(async () => {
    let result = await testAsync(1);
    console.log(result);
})();

console output:控制台 output:

test param is equal to 1
test param is smaller than 10
first IF

My problem is that if I structure the code in a way that includes more calls to resolve() that are not exclusive, the code does resolves the first one it hits but it does not stop and return in that place.我的问题是,如果我以一种包含更多非排他性的resolve()调用的方式构建代码,则代码会解析它遇到的第一个调用,但不会停止并返回该位置。 Why does it continue?为什么会继续? Is resolve() not loosely equal to return ? resolve()不是松散地等于return吗? Should I just add return after every resolve() call?我应该在每次resolve()调用后添加return吗?

return new Promise(async (resolve, reject) => { is antipattern. Adding async in front of a function will already return a Promise. new Promise has to be only used to convert a none Promise base async function into one that uses a Promise. return new Promise(async (resolve, reject) => { is antipattern. Adding async in front of a function will already return a Promise. new Promise has to be only used to convert a none Promise base async function into one that uses a Promise .

Is resolve() not loosely equal to return? resolve() 不是松散地等于返回吗?

It is only equal in the sense, that it sets the "return" value of the promise, but it does not break the execution flow.它只是在某种意义上相等,它设置了 promise 的“返回”值,但它不会中断执行流程。 So yes you would need to place a return after a resolve if you want to prevent that the following up code is executed.所以是的,如果您想防止执行后续代码,您需要在解析后return

But as I already said, the shown construct is anyway an anti-pattern and testAsync should look like this:但正如我已经说过的,显示的构造无论如何都是反模式,并且testAsync应该如下所示:

async function testAsync(testParam) {
  await wait(2000); // do some async work

  if (testParam === 1) {
    console.log("test param is equal to 1");
    return "first IF";
  }

  if (testParam < 10) {
    console.log("test param is smaller than 10");
    return "second IF";
  }
}

And the wait would more likely be written that way: wait更可能是这样写的:

function wait(time) {
    return new Promise((resolve, reject) => { setTimeout(resolve, time) });
}

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

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