返回的 Promise 有什么区别? [英] What is the difference between returned Promise?

查看:30
本文介绍了返回的 Promise 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有两种情况,都返回 Promise 并与以下 then 方法链接.但结果顺序不同.一些注意事项:Promise.resolve(value) -> 立即返回带有值的已完成 Promise.在 then 方法中,当我们再次返回 value 时,它​​会返回已完成的 Promise 与该值.逻辑上应该没有任何区别.两者都是即时...提前致谢...

There are two cases, both return Promise and are chained with the following then methods. But the result sequence is different. Some notes: Promise.resolve(value) -> returns immediately fulfilled Promise with the value. And in the then method when we return value again it returns fulfilled Promise with the value. Logically there shouldn't be any difference. Both are immediate... Thanks in advance...

Promise.resolve(1)
  .then((v) => {
    console.log(v);
    return Promise.resolve(v + 1);
  })
  .then((v) => {
    console.log(v);
    return Promise.resolve(v + 1);
  })
  .then((v) => {
    console.log(v);
    return v + 1;
  })
  .then((v) => {
    console.log(v);
    return v + 1;
  });

Promise.resolve(10)
  .then((v) => {
    console.log(v);
    return v + 1;
  })
  .then((v) => {
    console.log(v);
    return v + 1;
  })
  .then((v) => {
    console.log(v);
    return v + 1;
  })
  .then((v) => {
    console.log(v);
    return v + 1;
  });

//Result on the console:
//1
//10
//11
//12
//2
//13
//3
//4

//

Promise.resolve(1)
  .then((v) => {
    console.log(v);
    return v + 1;
  })
  .then((v) => {
    console.log(v);
    return v + 1;
  })
  .then((v) => {
    console.log(v);
    return v + 1;
  })
  .then((v) => {
    console.log(v);
    return v + 1;
  });

Promise.resolve(10)
  .then((v) => {
    console.log(v);
    return v + 1;
  })
  .then((v) => {
    console.log(v);
    return v + 1;
  })
  .then((v) => {
    console.log(v);
    return v + 1;
  })
  .then((v) => {
    console.log(v);
    return v + 1;
  });

//Result on the console:
//1
//10
//2
//11
//3
//12
//4
//13

推荐答案

有一个重要的区别:记住 then 返回一个 promise(我们称之为 promise T).当你这样做

There is an important difference: Remember that then returns a promise (let's call it promise T). When you do

return Promise.resolve(v + 1);

...您正在将承诺 T 解析为承诺(我们称之为承诺 B).但是当你这样做时:

...you're resolving promise T to a promise (let's call it promise B). But when you do:

return v + 1;

...您正在将 promise T 解析为立即值.

...you're resolving promise T to an immediate value.

将 promise T 解析为 promise B 会在解析周期中引入一个额外的滴答".不是承诺 T 将调用排队到其解析处理程序,它必须等到承诺 B 调用解析处理程序 T 必须在其上设置,然后然后调用其解析处理程序.因此,额外的滴答"(基本上是微任务队列的另一个循环).

Resolving promise T to promise B introduces an extra "tick" in the resolution cycle. Instead of promise T queuing a call to its resolution handler, it has to wait until promise B calls the resolution handler T has to set on it, and then call its resolution handler. Hence the extra "tick" (basically, another cycle of the microtask queue).

更简单的例子,注意它在First 2"之前记录Second 2"*:

Simpler example, note that it logs "Second 2" *before "First 2":

Promise.resolve(1)
    .then(v => Promise.resolve(v + 1))
    .then(v => console.log(`First: ${v}`));
Promise.resolve(1)
    .then(v => v + 1)
    .then(v => console.log(`Second: ${v}`));

...而如果你没有额外的承诺,它会在第二个 2"之前记录第一个 2":

...whereas if you don't have the extra promise, it logs "First 2" before "Second 2":

更简单的例子,注意它在First 2"之前记录Second 2"*:

Simpler example, note that it logs "Second 2" *before "First 2":

Promise.resolve(1)
    .then(v => v + 1)
    .then(v => console.log(`First: ${v}`));
Promise.resolve(1)
    .then(v => v + 1)
    .then(v => console.log(`Second: ${v}`));

最近为 async 函数删除了这个额外的勾号,这些函数执行诸如 return await somePromise(而不仅仅是 return somePromise),因为它可以对于通过 async/await 在内部处理的本机承诺,可以可靠地删除.但是我不确定它是否可以为您的案例可靠地删除,和/或这样做是否会引起必须这样做的人的任何重大关注.它需要处理从 then 处理程序返回的 native 承诺与任何其他 thenable¹ 不同,这可能是有问题的.(但我不确定它会是.)

This extra tick was recently removed for async functions that do things like return await somePromise (instead of just return somePromise) because it can reliably be removed for native promises handled internally via async/await. But I'm not sure it can be reliably removed for your case, and/or whether doing so will receive any significant attention from those who'd have to do it. It would require treating a native promise returned from a then handler different from any other thenable¹, which could be problematic. (But I don't know for sure that it would be.)

¹ thenablepromise:https://promisesaplus.com

这篇关于返回的 Promise 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆