Skip to content

Instantly share code, notes, and snippets.

@getify
Last active August 29, 2015 14:01
Show Gist options
  • Save getify/d64bb01751b50ed6b281 to your computer and use it in GitHub Desktop.
Save getify/d64bb01751b50ed6b281 to your computer and use it in GitHub Desktop.
moar promise bugs :/
// This bug has already been reported to clarify the ES6 spec:
// https://bugs.ecmascript.org/show_bug.cgi?id=2837
//
// I think it still needs to be reported to FF.
//
// Documenting here for completeness and for relationship to
// the other bugs here.
function immed(s) { x++; s(); }
function incX(){ x++; }
var x = 0;
var thenable = { then: immed };
var results = [];
Promise.resolve(thenable).then(incX);
results.push(x);
// check what happens after all "next cycle" steps
// have had a chance to complete
setTimeout(function(){
results.push(x);
console.log(results);
},100);
// FF: [1,2] --> incorrect i believe
// Ch: [0,2] --> correct i believe
// This bug may be related to the first one, because race() may
// use resolve() internally.
function immed(s) { x++; s(); }
function incX(){ x++; }
var x = 0;
var thenables = [
{ then: immed },
{ then: immed },
{ then: immed },
{ then: immed },
{ then: immed }
];
var results = [];
Promise.race(thenables).then(incX);
results.push(x);
// check what happens after all "next cycle" steps
// have had a chance to complete
setTimeout(function(){
results.push(x);
console.log(results);
},100);
// FF: [5,6] --> incorrect i believe
// Ch: [0,6] --> correct i believe
// This bug may be related to the first one, because all() may
// use resolve() internally.
function immed(s) { x++; s(); }
function incX(){ x++; }
var x = 0;
var thenables = [
{ then: immed },
{ then: immed },
{ then: immed },
{ then: immed },
{ then: immed }
];
var results = [];
Promise.all(thenables).then(incX);
results.push(x);
// check what happens after all "next cycle" steps
// have had a chance to complete
setTimeout(function(){
results.push(x);
console.log(results);
},100);
// FF: [5,6] --> incorrect i believe
// Ch: [0,6] --> correct i believe
// This (caching) bug appears only in Chrome IIUC. FF's output is also
// wrong, but it appears it's wrong because of the same bug as the previous
// three cases are showing.
//
// The spec says a Promise must not change its value once resolved, so a
// natural, though not required, assumption is that you can cache that
// value and not need to call `then(..)` on the promise again. Chrome
// appears to indeed be caching these results, which is fine.
//
// However, Chrome is also caching the results of thenables, which is
// NOT allowed by the spec, which is what's demonstrated here in this
// bug.
function immed(s) { x++; s(); }
function incX(){ x++; }
var x = 0;
var thenable = { then: immed };
var results = [];
for (var i=0; i<3; i++) {
Promise.resolve(thenable).then(incX);
results.push(x);
}
setTimeout(function(){
results.push(x);
console.log(results);
},100);
// FF: [1,2,3,6] --> incorrect i believe
// Ch: [0,0,0,4] --> incorrect i believe
// should be: [0,0,0,6]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment