简体   繁体   English

等待嵌套的JS诺言完成,然后再解决原始诺言

[英]Wait for nested JS promise to finish before resolving original promise

I am new to Promises and I'm having a little trouble with the concept of waiting for a nested promise to finish all executions before resolving the original promise. 我是Promises的新手,在解决嵌套的Promise之前要等待嵌套的Promise完成所有执行工作的概念时遇到了一些麻烦。

Original Code 原始码

function getSomething(text) {
    return new Promise(function (resolve, reject) {
        getElse(text).then(function (items) {
            if (items.length !== 0) {

                /* Stuff here */

                getElseElse(box).then(function (moreItems) {

                    /* Stuff here */

                    return array;
                }.then(function (array) {
                    var promise = new Promise(function (resolve, reject) {
                        /* anotherFunction() is asynchronous */
                        result = anotherFunction(array); <-- Spot 1
                    });
                    promise.then(function () { });
                });

                return resolve(result); <--- Spot 2

            }
            else {
                return resolve(null);
            }
        });
    });
};

Updated Code - better but still not fully working like I want. 更新了代码 -更好,但仍无法完全按照我的要求工作。

function getSomething(text) {
    return getElse(text).then(function (items) {
        if (items.length !== 0) {

            /* Stuff here */

            return getElseElse(box).then(function (moreItems) {

                /* Stuff here */

                return array;
            }).then(anotherFunction);

        } else {
            return null;
        }
    });
}

Then, inside of the individual View page, I have this: 然后,在各个“视图”页面中,我有以下内容:

getSomething(text).then(function (result) {
    /* Do something here using result */
    /* But result is undefined and I think still pending */
});

I have optimized the original function using Thomas's help but the original call inside the view still seems to be continuing before result is formulated. 我已经在Thomas的帮助下优化了原始功能,但是在公式化result之前,视图内的原始调用似乎仍在继续。

I want getSomething() to full complete and return result before I execute the code inside of the .then that is inside the view. 我想getSomething()以充分完整和返回result之前,我执行的内部代码.then这就是视图内。 So far, that is not being accomplished. 到目前为止,这还没有完成。

I have found a couple posts that have pointed me in what I think is the correct direction of Promise.all but I can't really seem to get anywhere with that information so I was hoping someone could help explain it for my specific situation. 我发现了一些帖子,指出了我认为Promise.all正确的方向。 Promise.all但是我似乎无法真正获得这些信息,因此我希望有人可以针对我的具体情况进行解释。

The posts that have helped are: 帮助的帖子是:

  1. Promise Chain not waiting for promises to resolve before ending 承诺链在结束之前不等待承诺解决
  2. Wait until nested promises resolve 等到嵌套的诺言解决
  3. Wait for promises inside Promise.all to finish before resolving it 等待Promise.all内部的诺言完成后再解决

Solution

The original issue was solved by Thomas in the marked answer. 最初的问题已由Thomas在标记的答案中解决。 The final problem was solved inside of anotherFunction() upon further inspection. 最后的问题在进一步检查后在anotherFunction()内部解决。

Originally: 本来:

function anotherFunction(array) {
    return new Promise(function (resolve, reject) {
        return anotherGet(parcels).then(function (list) {
            /* Do stuff here without a return */
        });
    });
}

Fixed Version: 固定版本:

function anotherFunction(array) {
    return anotherGet(parcels).then(function (list) {
        /* Do stuff here */
        return list;
    });
}

First, avoid the Promise/Deferred antipattern. 首先,避免Promise / Deferred反模式。 Rarely you need to create your own promises, usually you have some function that returns a Promise; 很少需要创建自己的Promise,通常您有一些返回Promise的函数; use that. 用那个。

Second, for the outer Promise to wait for a nesed PRomise, you need to return that nested Promise to the outer Promise. 其次,为了让外部Promise等待嵌套的Promise,您需要将嵌套的Promise返回给外部Promise。
Returning a Promise inside of then() will make the resulting Promise to resolve to the value of the Promise you retuned inside. then()内部返回Promise将使最终的Promise解析为您在其中重新调整的Promise的值。

And last, something like .then(value => value) or .then(value => Promise.resolve(someFunction(value))) is pointless. 最后,类似.then(value => value).then(value => Promise.resolve(someFunction(value)))的东西毫无意义。 Your wrapper around anotherFunction basically just passed through the argument and returned the result. 您对anotherFunction包装基本上只是通过参数传递并返回了结果。

So, your Pseudocode should look something like this: 因此,您的伪代码应如下所示:

function getSomething(text) {
  return getElse(text).then(function (items) {
    if (items.length !== 0) {
      /* Stuff here */
      return getElseElse(box).then(function (moreItems) {
        /* Stuff here */
        return array;
      }).then(anotherFunction);
    } else {
      return null;
    }
  });
}

The basic trick is to make sure all of your resolve() calls are inside of the nested promises' then() methods. 基本技巧是确保所有的resolve()调用都在嵌套的promise的then()方法内。

Something along the lines of: 类似于以下内容:

function getSomething(text) {
    return new Promise(function (resolve, reject) {
        getElse(text).then(function (items) {
            if (items.length !== 0) {

                /* Stuff here */

                getElseElse(box).then(function (moreItems) {

                    /* Stuff here */

                    return array;
                }.then(function (array) {
                    var promise = new Promise(function (resolve, reject) {
                        result = anotherFunction(array); <-- Spot 1
                    });
                    promise.then(function () { });

                    resolve(result); <--- Spot 2
                });    
            }
            else {
                resolve(null);
            }
        });
    });
};

If it needs to be nested in the promise.then() above, just stick it in there instead. 如果需要嵌套在上述promise.then() ,只需将其粘贴在其中即可。

Also note that you don't need to return your resolves. 另请注意,您无需返回您的解决方案。 Just call them. 打电话给他们。

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

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