Bug 160857

Summary: Promise API does not fully process microtask checkpoint: spec violation
Product: WebKit Reporter: Aleksandar Totic <a>
Component: JavaScriptCoreAssignee: Nobody <webkit-unassigned>
Status: NEW ---    
Severity: Normal CC: d, ggaren, sam, youennf
Priority: P2    
Version: WebKit Nightly Build   
Hardware: Unspecified   
OS: Unspecified   
See Also: https://bugs.webkit.org/show_bug.cgi?id=140043
Attachments:
Description Flags
This file will demonstrate the bug none

Description Aleksandar Totic 2016-08-15 12:19:08 PDT
Created attachment 286076 [details]
This file will demonstrate the bug

Promises microtask queue does not fully process microtasks queued while microtask is running.
According to spec, microtask queue should loop until it is empty.

Why is this bug important?
Promises are often used by developers for scheduling. When they are,
developers assume Promise.resolve() means "execute this function right 
after this script returns". This bug violates this promise.

FF/Edge/Chrome get this right. It'd be great if all browsers worked the same.

Example:

Promise.resolved()
 .then(() => {
   console.log("Promise 1");
   return Promise.resolve();
 })
 .then(() => {
   console.log("Promise 2");
 }
window.setTimeout(function() {
  console.log('timeout');
}, 0);

Might also be related to Bug #140043. I've attached a test case.
Comment 1 Aleksandar Totic 2016-08-16 13:47:01 PDT
Noticed that my example was not clear. The example should print: 
Promise 1
Promise 2
timeout

In Safari, it prints
Promise 1
timeout
Promise 2