Promise.resolve 与 Promise.resolve().then()
Promise.resolve vs Promise.resolve().then()
之前在这里问过一个问题,标题与这个问题完全相同,回答是“你不应该使用那个,而是使用这个”,我想知道它的作用,而不是我还能做什么做,是理解,不是简单的复制粘贴。
我的问题很简单,这三种方式在创建promise时有什么区别?
const API = (item, fail) =>
new Promise((resolve, reject) => {
if (fail) reject(item + ' ...with an error');
setTimeout(() => resolve(item), 1000);
});
(async () => {
const pro1 = Promise.resolve(API('I am inside resolve'));
const pro2 = Promise.resolve(API('I am inside resolve', true));
const pro3 = Promise.resolve().then(() => API('I am thenable'));
const pro4 = Promise.resolve().then(() => API('I am thenable', true));
const pro5 = new Promise((resolve) => resolve(API('I am a new promise')));
const pro6 = new Promise((resolve) => resolve(API('I am a new promise', true)));
const store = [pro1, pro2, pro3, pro4, pro5, pro6];
const results = await Promise.allSettled(store);
for (const { status, value, reason } of results) {
if (status === 'fulfilled') console.log(value)
else console.log(reason)
}
})();
Promise.resolve().then()
在您的示例中,then()
没有任何区别,因为您只是解决承诺并获取其数据。
then()
通常用于链式承诺,以此为例:
Promise.resolve('foo')
// 1. Receive "foo", concatenate "bar" to it, and resolve that to the next then
.then(function(string) {
return new Promise(function(resolve, reject) {
setTimeout(function() {
string += 'bar';
resolve(string);
}, 1);
});
})
// 2. receive "foobar", register a callback function to work on that string
// and print it to the console, but not before returning the unworked on
// string to the next then
.then(function(string) {
setTimeout(function() {
string += 'baz';
console.log(string); // foobarbaz
}, 1)
return string;
})
在这里,我们将多个承诺 then()
从先前已解决的承诺中链接出来,同时保留第一个解决的原始数据,但在这种情况下,我们会根据每个新承诺对其进行修改。
您可以阅读有关承诺和链接的更多信息here。
区别在于要完成的工作。虽然所有这些方法都是有效的,但它们具有不同的成本和可预测性。
Promise.resolve()
生成单个解析的 Promise 实例,并且根据提供给调用的值,JS 引擎有信息来优化它。它使得所有工作都可以在对 JS 引擎(通常是 C++,但可以是 Java 或 WASM)的底层代码的一次调用中完成。所以它永远是最好的选择。
Promise.resolve().then(() => API(/*...*/))
生成多个 Promise 实例:一个在 Promise.resolve()
时,另一个在 .then()
调用时。它还分配更多内存并在 JS 和引擎之间进行多次(3 次或更多次)冗余跳转。它很难优化,需要执行密集的试探法才能确定此调用是否可优化。这是最糟糕的选择。
new Promise((resolve) => resolve(API(/* ... */))
分配一个函数和一个Promise实例,并在JS和引擎之间进行两次跳转。由于 JS 的性质,很难优化此调用。
之前在这里问过一个问题,标题与这个问题完全相同,回答是“你不应该使用那个,而是使用这个”,我想知道它的作用,而不是我还能做什么做,是理解,不是简单的复制粘贴。
我的问题很简单,这三种方式在创建promise时有什么区别?
const API = (item, fail) =>
new Promise((resolve, reject) => {
if (fail) reject(item + ' ...with an error');
setTimeout(() => resolve(item), 1000);
});
(async () => {
const pro1 = Promise.resolve(API('I am inside resolve'));
const pro2 = Promise.resolve(API('I am inside resolve', true));
const pro3 = Promise.resolve().then(() => API('I am thenable'));
const pro4 = Promise.resolve().then(() => API('I am thenable', true));
const pro5 = new Promise((resolve) => resolve(API('I am a new promise')));
const pro6 = new Promise((resolve) => resolve(API('I am a new promise', true)));
const store = [pro1, pro2, pro3, pro4, pro5, pro6];
const results = await Promise.allSettled(store);
for (const { status, value, reason } of results) {
if (status === 'fulfilled') console.log(value)
else console.log(reason)
}
})();
Promise.resolve().then()
在您的示例中,then()
没有任何区别,因为您只是解决承诺并获取其数据。
then()
通常用于链式承诺,以此为例:
Promise.resolve('foo')
// 1. Receive "foo", concatenate "bar" to it, and resolve that to the next then
.then(function(string) {
return new Promise(function(resolve, reject) {
setTimeout(function() {
string += 'bar';
resolve(string);
}, 1);
});
})
// 2. receive "foobar", register a callback function to work on that string
// and print it to the console, but not before returning the unworked on
// string to the next then
.then(function(string) {
setTimeout(function() {
string += 'baz';
console.log(string); // foobarbaz
}, 1)
return string;
})
在这里,我们将多个承诺 then()
从先前已解决的承诺中链接出来,同时保留第一个解决的原始数据,但在这种情况下,我们会根据每个新承诺对其进行修改。
您可以阅读有关承诺和链接的更多信息here。
区别在于要完成的工作。虽然所有这些方法都是有效的,但它们具有不同的成本和可预测性。
Promise.resolve()
生成单个解析的 Promise 实例,并且根据提供给调用的值,JS 引擎有信息来优化它。它使得所有工作都可以在对 JS 引擎(通常是 C++,但可以是 Java 或 WASM)的底层代码的一次调用中完成。所以它永远是最好的选择。Promise.resolve().then(() => API(/*...*/))
生成多个 Promise 实例:一个在Promise.resolve()
时,另一个在.then()
调用时。它还分配更多内存并在 JS 和引擎之间进行多次(3 次或更多次)冗余跳转。它很难优化,需要执行密集的试探法才能确定此调用是否可优化。这是最糟糕的选择。new Promise((resolve) => resolve(API(/* ... */))
分配一个函数和一个Promise实例,并在JS和引擎之间进行两次跳转。由于 JS 的性质,很难优化此调用。