以下有什么区别: 在我看来,他们都返回了一个已解决的 promise ,但我认为这可能不正确。我想知道它们是否有区别?
getQuery = function () {
return Promise.resolve({
name: "some query name";
});
}
executeQuery = function () {
return new Promise((resolve, reject) => {
resolve(mockQueryResult);
}); },
最佳答案
查看 ECMAScript 2015 specification (强调我的):
The resolve function returns either a new promise resolved with the passed argument, or the argument itself if the argument is a promise produced by this constructor.
然后在实现中进一步阅读:
6. Let resolveResult be Call(promiseCapability.[[Resolve]], undefined, «x»).
我相信这意味着根据规范,两者的结果是相同的。当然,如果您使用的是第三方库或 polyfill,您可能会得到不同的结果。
关于javascript - 返回已解决的 promise 和仅处理解决的常规 promise 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54314417/