我刚刚读完了Promises/A +规范并偶然发现了微任务和macrotask这两个术语:请参阅http://promisesaplus.com/#notes
我以前从未听说过这些术语,现在我很好奇它的区别是什么?
我已经尝试在网上找到一些信息,但我发现的所有内容都来自w3.org档案馆(这并不能解释我与众不同之处):http://lists.w3.org/Archives /Public/public-nextweb/2013Jul/0018.html
另外,我发现了一个名为"macrotask"的npm模块:https://www.npmjs.org/package/macrotask 同样,没有明确区别的是什么.
我所知道的是,它与事件循环有关,如https://html.spec.whatwg.org/multipage/webappapis.html#task-queue 和https://html.spec.whatwg中所述. .ORG /多页/ webappapis.html#执行-A-microtask检查点
根据WHATWG规范,我知道理论上我应该能够自己提取差异.但我确信其他人也可以从专家的简短解释中受益.
我无法理解以下代码是如何运行的.为什么"1"在"b"之后但"h"在"3"之后?订单应该是:a,b,1,2,h,3?有些文章说"事件循环队列"和"作业队列"之间的区别导致了以下输出.但是怎么样?我已经阅读了ECMAScript 2015 - 8.4工作和工作队列的规范,想知道Promise'job是如何工作的,但这让我更加困惑.有人能帮我吗?谢谢!
var promise = new Promise(function(resolve, reject) {resolve(1)});
promise.then(function(resolve) {console.log(1)});
console.log('a');
promise.then(function(resolve) {console.log(2);});
setTimeout(function() {console.log('h')}, 0);
promise.then(function(resolve) {console.log(3)});
console.log('b');
// a
// b
// 1
// 2
// 3
// h
Run Code Online (Sandbox Code Playgroud)
我知道Promise是异步的,但setTimeout(..)异步操作的回调总是在Promise的异步操作之后.为什么?
我有以下代码:
var incr = num => new Promise(resolve => {
resolve(num + 1);
});
var x = incr(3)
.then(resp => incr(resp))
.then(resp => console.log(resp));
async function incrTwice(num) {
const first = await incr(num);
const twice = await incr(first);
console.log(twice);
}
incrTwice(6);Run Code Online (Sandbox Code Playgroud)
我相信(可能是错误的)显示了实现相同功能的两种等效方法:首先通过链接承诺,其次使用 async/await 的语法糖。
我希望promise 链接解决方案首先是console.log,然后是异步函数,但是异步函数console.log 的第一个然后是promise 链接解决方案打印。
我的逻辑如下:
xs 初始解析将在处理声明时首先出现在微任务队列中x和的声明之间堆栈是空的,incrTwice这将导致微任务队列被刷新(导致承诺链的完成)
incrTwice 被定义为incrTwice在awaits处的微任务队列上执行排队,最终打印到控制台
显然我在某处有误解,有人能够指出我错在哪里吗?
我正在阅读一篇关于 javascript 承诺的文档(https://developers.google.com/web/fundamentals/getting-started/primers/promises),其中一个示例使用了一系列承诺。
// Start off with a promise that always resolves
var sequence = Promise.resolve();
// Loop through our chapter urls
story.chapterUrls.forEach(function(chapterUrl) {
// Add these actions to the end of the sequence
sequence = sequence.then(function() {
return getJSON(chapterUrl);
}).then(function(chapter) {
addHtmlToPage(chapter.html);
});
})
Run Code Online (Sandbox Code Playgroud)
我很好奇它是如何工作的,因为我假设当第一个 .then 添加到承诺序列中时它将开始执行代码。当我调试代码时,直到脚本标记中的最后一行代码被执行后,承诺序列才被执行。所以我的问题是承诺何时真正得到执行?谢谢。
我在Node和Chrome中看到此行为:
setTimeout(()=>{ console.log('timeout') }, 0)
Promise.resolve().then(()=>{ console.log('promise') })
console.log('sync')
// output order:
// sync
// promise
// timeout
Run Code Online (Sandbox Code Playgroud)
我的问题是,这是一致的行为吗?即,根据规范,是否已在已记忆/已解决的承诺中then或await之前始终触发setTimeout(fn, 0)?
我想在以下内容中使用它,如果我的承诺中有记忆的结果,则返回一件事,否则返回:
// somewhere during object initialization
this.resultingPromise = expensiveAsyncFunction()
// in a method called frequently
Promise.race([
new Promise(resolve => setTimeout(() => resolve('default'), 0)),
this.resultingPromise
])
Run Code Online (Sandbox Code Playgroud) 对于以下代码
function inner () {
new Promise(function(resolve,reject){
resolve()
}).then(function(){
console.log('Inner Promise')
})
}
function outer() {
return new Promise(function(resolve, reject){
resolve()
inner()
})
}
outer().then(function(data) {
console.log('Outer Promise')
})
Run Code Online (Sandbox Code Playgroud)
输出是
Inner Promise
Outer Promise
Run Code Online (Sandbox Code Playgroud)
我认为外部决心将是第一个进入JS Message Queue,然后是内部解析.然而,JS事件循环首先触发内部解析,然后是外部解析.
Promise在内部解决了什么?