That was on Friday. I still don’t know why the bug happens, but I can reproduce it. Evidence says it’s a Chrome 52 bug, not a general JavaScript bug. Neither Safari nor Firefox make it happen.

Sometimes when you call .fetch() on a Backbone model, callbacks don’t fire. There’s no JavaScript error, the REST call works, the result is vanished by the browser. Joy of joys.

2 fetches, 1 callback

You need:

  • Webpack
  • Babel 6
  • Backbone (maybe Backbone is doing it wrong?)
  • A server that can serve JSON files
  • 14 lines of code

I used create-react-app to set up a basic environment, ejected with npm run eject and added some config to Webpack to allow jQuery. Backbone depends on it. This part is not that relevant.

To show the bug itself, we need a Backbone model:

// A basic Backbone model
class BugModel extends Backbone.Model {
    constructor() {
        super();
 
        this.url = "bla.json";
    }
}

Now I know what you’re thinking, that’s an ES6 object and Backbone was built before ES6. Official docs never mention the new class syntax. The traditional Backbone approach works too:

var BugModel = Backbone.Model.extend({
    url: "bla.json"
});

The same bug happens. I tried.

To cause the bug, we have to call .fetch() on two instances of BugModel that share the same id. The code looks like this:

let bug = new BugModel();
bug.fetch({
    success: () => {
        console.log('fetch 1'); // this happens
        doWeirdness(bug);
    }
});
 
function doWeirdness(bug) {
    let newBug = new BugModel({id: bug.id});
 
    console.log('about to re-fetch');
 
    newBug.fetch({
        success: () => console.log('fetch 2', newBug), // don't get here
        error: () => console.log('error')
    });
}

We create an instance of BugModel in a variable called bug, then call .fetch(). This makes an API call to /bla.json, which returns a JSON file that looks like this: {"id": 1, "hai": "hello"}.

Backbone automagically parses this JSON string and sets an id and hai attribute. This part works like a charm.

Then we call doWeirdness and pass our bug object as an attribute. Inside doWeirdness, we make a new instance of BugModel called newBug and give it the same id as bug had. This is a crucial step.

When we call fetch on this new instance, the API call happens, but the callbacks do not. 'fetch 2' never prints.

This whole sequence of events might sound redundant, but it made sense in my real codebase. Even if re-fetching objects doesn’t make sense, it should work. It works in both Safari and Firefox.

Works in Safari Works in Firefox

Clues about why

This smells like a bug in memory optimization. Chrome thinks bug and newBug are the same object, even though they are not.

If you call fetch() twice on the same instance of BugModel, the same bug happens. (clue, part 1)

If both instances have a different id, both fetch calls fire callbacks. (clue, part 2)

Curiously, if you add a third fetch() call, that works.

    newBug.fetch({
        success: () => console.log('fetch 2', newBug), // don't get here
        error: () => console.log('error')
    });
 
    newBug.fetch({
        success: () => console.log('fetch 3') // prints
    });

The bug didn’t happen in Chrome 51.

I am confused. ?

PS: there is a followup

Learned something new? Want to improve your skills?

Join over 10,000 engineers just like you already improving their skills!

Here's how it works 👇

Leave your email and I'll send you an Interactive Modern JavaScript Cheatsheet 📖right away. After that you'll get thoughtfully written emails every week about React, JavaScript, and your career. Lessons learned over my 20 years in the industry working with companies ranging from tiny startups to Fortune5 behemoths.

PS: You should also follow me on twitter 👉 here.
It's where I go to shoot the shit about programming.