在解决原始承诺之前等待嵌套的 JS 承诺完成 [英] Wait for nested JS promise to finish before resolving original promise
问题描述
我是 Promises 的新手,我在解决原始承诺之前等待嵌套承诺完成所有执行的概念有点麻烦.
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.
原始代码
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 */
});
我已经使用 Thomas 的帮助优化了原始函数,但在制定 result
之前,视图中的原始调用似乎仍在继续.
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.
我希望 getSomething()
在执行视图内的 .then
内的代码之前完成并返回 result
.到目前为止,这还没有完成.
I want getSomething()
to full complete and return result
before I execute the code inside of the .then
that is inside the view. So far, that is not being accomplished.
我发现了一些帖子,它们指出了我认为 Promise.all
的正确方向,但我似乎无法获得这些信息,所以我希望有人可以帮助解释我的具体情况.
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.
有帮助的帖子是:
解决方案
托马斯在标记答案中解决了原始问题.经过进一步检查,最终问题在 anotherFunction()
内部得到解决.
原来:
function anotherFunction(array) {
return new Promise(function (resolve, reject) {
return anotherGet(parcels).then(function (list) {
/* Do stuff here without a return */
});
});
}
固定版本:
function anotherFunction(array) {
return anotherGet(parcels).then(function (list) {
/* Do stuff here */
return list;
});
}
推荐答案
首先,避免 Promise/Deferred 反模式.您很少需要创建自己的 Promise,通常您有一些返回 Promise 的函数;用那个.
First, avoid the Promise/Deferred antipattern. Rarely you need to create your own promises, usually you have some function that returns a Promise; use that.
其次,为了让外层 Promise 等待一个需要的 PROmise,你需要将嵌套的 Promise 返回给外层 Promise.
在 then()
中返回一个 Promise 将使生成的 Promise 解析为您在内部重新调整的 Promise 的值.
Second, for the outer Promise to wait for a nesed PRomise, you need to return that nested Promise to the outer Promise.
Returning a Promise inside of then()
will make the resulting Promise to resolve to the value of the Promise you retuned inside.
最后,类似于 .then(value => value)
或 .then(value => Promise.resolve(someFunction(value)))
是无意义.您对 anotherFunction
的包装基本上只是通过了参数并返回了结果.
And last, something like .then(value => value)
or .then(value => Promise.resolve(someFunction(value)))
is pointless. Your wrapper around anotherFunction
basically just passed through the argument and returned the result.
因此,您的伪代码应如下所示:
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;
}
});
}
这篇关于在解决原始承诺之前等待嵌套的 JS 承诺完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!