在解决原始承诺之前等待嵌套的 JS 承诺完成

Han*_*ank 0 javascript nested promise

我是 Promises 的新手,我在解决原始承诺之前等待嵌套承诺完成所有执行的概念有点麻烦。

原始代码

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);
            }
        });
    });
};
Run Code Online (Sandbox Code Playgroud)

更新的代码- 更好,但仍然没有像我想要的那样完全工作。

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;
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

然后,在单个视图页面内,我有这个:

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

我已经使用 Thomas 的帮助优化了原始函数,但视图内部的原始调用似乎在result制定之前仍在继续。

我想在执行视图内的代码之前getSomething()完全完成并返回。到目前为止,这还没有完成。result.then

我发现了一些帖子,它们为我指出了我认为正确的方向,Promise.all但我似乎无法获得这些信息,所以我希望有人能帮助解释我的具体情况。

有帮助的帖子是:

  1. Promise Chain 在结束之前不等待 Promise 解决
  2. 等到嵌套的承诺解决
  3. 在解决之前等待 Promise.all 中的 promises 完成

解决方案

Thomas 在标记答案中解决了原始问题。anotherFunction()经过进一步检查,最终问题在内部解决了。

起初:

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

固定版本:

function anotherFunction(array) {
    return anotherGet(parcels).then(function (list) {
        /* Do stuff here */
        return list;
    });
}
Run Code Online (Sandbox Code Playgroud)

Tho*_*mas 5

首先,避免 Promise/Deferred 反模式。您很少需要创建自己的 Promise,通常您有一些返回 Promise 的函数;用那个。

其次,为了让外部 Promise 等待一个 nesed PROmise,您需要将该嵌套 Promise 返回给外部 Promise。
返回其中的 Promisethen()将使生成的 Promise 解析为您在内部重新调整的 Promise 的值。

最后,像.then(value => value)或这样的东西.then(value => Promise.resolve(someFunction(value)))毫无意义。您的包装器anotherFunction基本上只是通过参数并返回结果。

所以,你的伪代码应该是这样的:

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;
    }
  });
}
Run Code Online (Sandbox Code Playgroud)