"executor" 和传递给 then() 方法的函数之间有什么关系(如果有的话)?
What is the relationship, if any, between the "executor" and the function passed to the then() method?
我指的“执行者”功能:
然后 then() 方法:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then
它们的含义或目的似乎有些重叠。
在这两种情况下,我们通常将它们的参数命名为“resolve”和“reject”-(编辑:这显然是错误的,我不知何故混淆了)。
我几乎认为传递给 then() 的函数以某种方式传递给了执行程序。
但事实似乎并非如此:
- 文档指出,传递给执行程序的函数是自动生成的:
At the time when the constructor generates the new Promise object, it also generates a corresponding pair of functions for resolutionFunc and rejectionFunc; these are "tethered" to the Promise object.
- 我们也可以在不调用 then() 方法的情况下完成承诺。
总结:
我认为在执行者的情况下,resolve 和 reject 函数是由构造函数生成的函数,但我们控制调用它们 来解决 承诺和结果。
我们编写并传递给 then() 的 resolve 和 reject 函数会自动调用,结果与之前相同,但是在 promise 确定后 .
这是正确的吗?
两者之间没有任何真正的联系,因为它们是完全不同的东西,目的完全不同。
执行器函数决定了 promise 何时最终解决或拒绝。这两个参数是您在 promise 应该解决(带有可选值)或应该拒绝(带有原因)时调用的函数。
传递给 .then()
的一两个参数是在承诺更改状态(已解决或已拒绝)时调用的侦听器。他们不确定承诺何时得到解决或拒绝,而是倾听何时发生。
Promise(在 10,000' 级别)只是一个用于异步事件完成或错误的监视系统。执行者确定 promise 何时完成,.then()
或 .catch()
处理程序允许您添加侦听器,以便在 promise 实现或被拒绝时收到通知。
I am thinking that in case of the executor, that the resolve and reject functions are functions generated by the constructor, but we control calling them to settle the promise with a result.
是的,没错 - 它们是在构造函数中生成的。您控制 when/if 他们被调用。
The resolve and reject functions we write and pass to then() are automatically called with the same result as before, but after the promise is settled.
将这些视为 onFulfilled
和 onRejected
侦听器函数。是的,promise结算的时候调用合适的。
其他几点。您可以与一个根本没有 .then()
的执行人做出承诺。例如,您可能只有一个 .catch()
,因为您只想知道此操作是否有错误。因此,即使没有任何 onFulfilled 侦听器,promise 执行器也能正常工作(因为它与侦听器无关)。
而且,如果您的代码的多个部分对这个承诺的结果感兴趣,那么您可以在同一个承诺上有多个 .then()
侦听器。您甚至可以在承诺已经实现或被拒绝后添加一个 .then()
侦听器,它将在事件循环的下一个周期中被调用。我提到这些只是为了进一步确认 .then()
听众和你从 promise 执行者那里得到的 resolve/reject 处理程序之间没有关联。
我指的“执行者”功能:
然后 then() 方法:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then
它们的含义或目的似乎有些重叠。
在这两种情况下,我们通常将它们的参数命名为“resolve”和“reject”-(编辑:这显然是错误的,我不知何故混淆了)。
我几乎认为传递给 then() 的函数以某种方式传递给了执行程序。
但事实似乎并非如此:
- 文档指出,传递给执行程序的函数是自动生成的:
At the time when the constructor generates the new Promise object, it also generates a corresponding pair of functions for resolutionFunc and rejectionFunc; these are "tethered" to the Promise object.
- 我们也可以在不调用 then() 方法的情况下完成承诺。
总结:
我认为在执行者的情况下,resolve 和 reject 函数是由构造函数生成的函数,但我们控制调用它们 来解决 承诺和结果。
我们编写并传递给 then() 的 resolve 和 reject 函数会自动调用,结果与之前相同,但是在 promise 确定后 .
这是正确的吗?
两者之间没有任何真正的联系,因为它们是完全不同的东西,目的完全不同。
执行器函数决定了 promise 何时最终解决或拒绝。这两个参数是您在 promise 应该解决(带有可选值)或应该拒绝(带有原因)时调用的函数。
传递给 .then()
的一两个参数是在承诺更改状态(已解决或已拒绝)时调用的侦听器。他们不确定承诺何时得到解决或拒绝,而是倾听何时发生。
Promise(在 10,000' 级别)只是一个用于异步事件完成或错误的监视系统。执行者确定 promise 何时完成,.then()
或 .catch()
处理程序允许您添加侦听器,以便在 promise 实现或被拒绝时收到通知。
I am thinking that in case of the executor, that the resolve and reject functions are functions generated by the constructor, but we control calling them to settle the promise with a result.
是的,没错 - 它们是在构造函数中生成的。您控制 when/if 他们被调用。
The resolve and reject functions we write and pass to then() are automatically called with the same result as before, but after the promise is settled.
将这些视为 onFulfilled
和 onRejected
侦听器函数。是的,promise结算的时候调用合适的。
其他几点。您可以与一个根本没有 .then()
的执行人做出承诺。例如,您可能只有一个 .catch()
,因为您只想知道此操作是否有错误。因此,即使没有任何 onFulfilled 侦听器,promise 执行器也能正常工作(因为它与侦听器无关)。
而且,如果您的代码的多个部分对这个承诺的结果感兴趣,那么您可以在同一个承诺上有多个 .then()
侦听器。您甚至可以在承诺已经实现或被拒绝后添加一个 .then()
侦听器,它将在事件循环的下一个周期中被调用。我提到这些只是为了进一步确认 .then()
听众和你从 promise 执行者那里得到的 resolve/reject 处理程序之间没有关联。