- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
下面的 resolve()
函数和 then()
函数是 ES6 Promise 机制中唯一查看并添加实现处理程序的函数吗?作业队列(并将它们标记为已添加且不再添加)?
let promise = new Promise(function(resolve, reject) {
console.log("resolve, reject are", resolve, reject);
setTimeout(function() {
resolve("this is the success data");
}, 1000);
});
promise.then(
function(a) {
console.log("Success.", a);
promise.then(function(a) { console.log("Go have a drink.", a); });
},
function(a) {
console.log("failure", a)
}
);
上述代码的替代方法是,resolve()
直接完成,而不是在 setTimeout()
中。 then(fn)
是否会将 fn
添加到此 promise 的履行处理程序表(而非作业队列)中,或者如果 promise 已经解决,直接将fn
添加到job queue,fn
标记为“已经添加到job queue”,不需要再次添加?
另一方面,resolve()
是 JS ES6 提供的函数,它将 promise 标记为“已解决”(状态),并检查此 promise 的履行处理程序表是否为空或不。如果有这样的处理程序(并且之前不应该被标记为已添加到作业队列),则将这些处理程序添加到作业队列并标记为已添加。
并且无论如何,假设 fulfillment handler 表中有 fn1
, fn2
, fn3
, ... 等,每一个作为 fn1(resolvedValue)
添加到作业队列中,其中 resolvedValue
由 resolve(resolveValue)
调用在内部记住?
并且根本没有访问 fulfillment handler 表的其他情况。作业队列作业由 JS 事件循环执行,因此当所有 GUI“单击事件处理程序”等完成后,作业队列中的履行(和拒绝)处理程序将被执行,然后是 setTimeout 和 setInterval处理程序也被执行?这就是履行处理程序表和作业队列的工作方式吗?
最佳答案
Is it true that the
resolve()
function andthen()
function below are the only functions in the ES6 Promise mechanism that look at and add the fulfillment handlers to the job queue?
是的。 (reject
还会查看表并删除处理程序而不将它们添加到作业队列中)。
Is it true that the
then(fn)
will add thefn
to the fulfillment handler table (not job queue) for this promise, or if the promise has already been resolved, directly add thefn
to the job queue?
是的。
and the
fn
is marked as "already added to job queue" and no need to be added again?
不,fn
根本没有标记。它可以多次传递给 then
,在这种情况下它需要运行多次。
但是,是的,当 promise 履行并安排回调作业时,履行处理程序表将被清除。
And in any event, suppose there are
fn1
,fn2
,fn3
, ... etc in the fulfillment handler table, each one is added to the job queue asfn1(resolvedValue)
, where theresolvedValue
is remembered internally by theresolve(resolveValue)
call?
是的。虽然技术上resolve
does not always fulfill the promise .
And there are no other situation where the fulfillment handler table is accessed at all.
是的。我想知道你为什么关心。
The job queue jobs are executed by the JS event loop, so that when all the GUI "click event handlers", etc are done, then the fulfillment (and rejection) handlers in the job queue are executed, and then also the setTimeout and setInterval handlers are also executed? Is this how the fulfillment handler table and job queue work?
是的。但是请注意 click handlers, setTimeout
and setInterval
have a separate event queue from promise jobs .
关于javascript - 什么时候检查 ES6 promise 履行处理程序表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59476887/
实现的在线 shopify 文档显示了这个例子: POST /admin/orders/#{id}/fulfillments.json { "fulfillment": { "tra
我有检查数组中值的函数 const arr = [10,20,30]; const check = (needle) => { return new Promi
promise.each(function(obj){ obj.doIntensiveWork() .then(function(){ console.log("I A
我正在从 Protractor 切换到 Nightwatch.js,我在 Nightwatch 处理 promise 的方式上遇到了一些困难。 举个例子,我正在尝试计算满足给定条件的元素的数量。计数函
我正在尝试使用 DialogFlow 实现 Webhook 来访问我的应用程序,但每当我发出请求时,它都会返回: POST /webhook 500 INTERNAL SERVER ERROR 来自n
使用 BlueBird promise ,我尝试将 getCredentials 变成一个将作为 getCredentials.then(function() { do this and that }
我正在使用 Dialogflow 和 Actions on Google 开发一个应用程序,该应用程序需要在 Google Assistant 中发送每日生日通知。 我正在关注this documen
对于摩卡测试,我想断言 promise 最终会拒绝。 我不想使用 chai-as-promised。我更愿意只使用 Node 的标准断言模块,并且只使用标准 ES6 Promise。 我想出的最好的就
我想用其他 promise 来履行 promise 。重点是,我真的很想在第一个 promise 实现后立即访问(仍在等待中的)第二个promise。不幸的是,我似乎只能在两个 promise 都实现
我正在使用 Node.js 开发一个使用用户证书的项目。我需要以阻塞方式同步生成它们,但我使用的库(pem)只有异步函数(回调)。我尝试了多种方法来解决这个问题,但我的尝试都没有成功。我的代码如下所示
我试图表达如下内容: it("should use the 'text' mode", () => { let usedMockMode = false; let env = new
我是一名优秀的程序员,十分优秀!