{"action":"opened","issue":{"url":"https://api.github.com/repos/zloirock/core-js/issues/85","labels_url":"https://api.github.com/repos/zloirock/core-js/issues/85/labels{/name}","comments_url":"https://api.github.com/repos/zloirock/core-js/issues/85/comments","events_url":"https://api.github.com/repos/zloirock/core-js/issues/85/events","html_url":"https://github.com/zloirock/core-js/issues/85","id":91099105,"number":85,"title":"Nested setTimeout calls cause invalid Promise behavior in Chrome","user":{"login":"loganfsmyth","id":132260,"avatar_url":"https://avatars.githubusercontent.com/u/132260?v=3","gravatar_id":"","url":"https://api.github.com/users/loganfsmyth","html_url":"https://github.com/loganfsmyth","followers_url":"https://api.github.com/users/loganfsmyth/followers","following_url":"https://api.github.com/users/loganfsmyth/following{/other_user}","gists_url":"https://api.github.com/users/loganfsmyth/gists{/gist_id}","starred_url":"https://api.github.com/users/loganfsmyth/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/loganfsmyth/subscriptions","organizations_url":"https://api.github.com/users/loganfsmyth/orgs","repos_url":"https://api.github.com/users/loganfsmyth/repos","events_url":"https://api.github.com/users/loganfsmyth/events{/privacy}","received_events_url":"https://api.github.com/users/loganfsmyth/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-06-25T23:06:04Z","updated_at":"2015-06-25T23:06:04Z","closed_at":null,"body":"Came across this when upgrading to Babel 5, picking up core-js 0.9.18, causing us to switch from Chrome promises to polyfilled promises.\r\n\r\nThere appears to be some very strange `setTimeout`/`postMessage` nesting behavior. I'm seeing\r\n\r\n```\r\nsetTimeout(function(){\r\n    setTimeout(function(){\r\n        setTimeout(function(){\r\n            setTimeout(function(){\r\n                Promise.reject('err').then(function(){}).then(function(){}).catch(function(err){\r\n                    console.log('handled error', err);\r\n                });\r\n            }, 0);\r\n        }, 500);\r\n    }, 0);\r\n}, 500);\r\n```\r\n\r\nin Chrome print out BOTH my own `console.log('handled error');` *AND* the internal `console.error('Unhandled promise rejection');` message. It seems to be somewhat flaky, and it almost always works when the devtools are open, but if you run that code in a standalone file with just core-js, and open devtools after running it, you'll see the error.\r\n\r\nObviously this code is really roundabout, but this came up for me in Jasmine because of code like\r\n\r\n    waits(500);\r\n    waits(500);\r\n    runs(function(){\r\n        // Some normal code that happens to create a promise chain like the one above.\r\n    });\r\n\r\nI did some testing on my own machine by splitting out the core-js code and just using straight JS:\r\n\r\n```\r\nsetTimeout(function(){\r\n    setTimeout(function(){\r\n        setTimeout(function(){\r\n            setTimeout(function(){\r\n                setTimeout(function(){\r\n                    console.log('2')\r\n                }, 1);\r\n\r\n                window.addEventListener('message', function fn(){\r\n                    window.removeEventListener('message', fn, false);\r\n\r\n                    console.log('1');\r\n                    setTimeout(function(){\r\n                        console.log('3');\r\n                    }, 1);\r\n                }, false);\r\n                window.postMessage('something', '*');\r\n            }, 0);\r\n        }, 500);\r\n    }, 0);\r\n}, 500);\r\n```\r\n\r\nprints \r\n\r\n```\r\n1\r\n3\r\n2\r\n```\r\n\r\nwhen I have devtools closed, which I think is making the promise code process the rejection in the wrong order."}}