1

I've to manage a project made with Backbone.js. The project was first released in 2013. At the moment I try to figure out what they did before.

I found out that they use quite often the Promise.js libery to communicate with the ReST-Server like the example underneath

run: function () {
    if (this.initialAppViewMethod) {
        Promise.resolve(this.model.get('session').fetch())
            .catch(this.showLoginView.bind(this))
            .then(this.initMainView.bind(this));
    }
},

I've been wondering if there is a certain reason why they have used Promise.js instead the already used jquery.js liberay to do promises?

BTW: How can I convert this script to use pure jQuery functionallity?

FaltFe
  • 195
  • 11
  • 2
    Because jQuery "promises" are [not pure](http://stackoverflow.com/q/23744612/1048572)? – Bergi Nov 23 '16 at 20:28
  • So if I understand this correct it is saver to use Promise.js as long as I use jQuery < 3.0 because jQuery don't handle promises always correct? – FaltFe Nov 23 '16 at 20:38
  • @FelixFaltin only if you use code that needs compliant promises. Otherwise, there are no reasons. – Emile Bergeron Nov 23 '16 at 20:44
  • Well, jQuery 3 did fix *some* issues, but plain promises are still easier to work with. – Bergi Nov 23 '16 at 20:47
  • @Bergi but seeing the example in the question, there are no apparent reasons. – Emile Bergeron Nov 23 '16 at 20:50
  • @EmileBergeron uh, a `.catch()` that works like expected looks like a very good reason to me? Although I have no idea what this particular usage is supposed to do – Bergi Nov 23 '16 at 20:52
  • @Bergi why? jQuery and Backbone (`error` callback) already provides that. That's a genuine question, what's the difference between jQuery 3 `catch` and Promises `catch`? What's better then say the Backbone `error` callback? – Emile Bergeron Nov 23 '16 at 20:55
  • @EmileBergeron Ah, I missed that they added `.catch` to jQuery 3 as well. But those definitely are better than `error` callbacks, especially because they [chain](http://stackoverflow.com/questions/16371129/chained-promises-not-passing-on-rejection). Notice that `initMainView` is *always* called (unless `showLoginView` throws), quite unlike the callbacks in your answer. – Bergi Nov 23 '16 at 21:02
  • @Bergi you're making me doubt myself, but wouldn't the `showLoginView` be called only if the `fetch` fails (promise rejected) and `initMainView` called only if the `fetch` succeed? Yeah chaining is definitely nice, but provided by the jqXHR by default. – Emile Bergeron Nov 23 '16 at 21:12
  • @EmileBergeron this was also my thought. That is is a kind of if-else statement. – FaltFe Nov 23 '16 at 21:19
  • 1
    @EmileBergeron No, `initMainView` is called whenever the `fetch(…).catch(…)` fulfills. See also [the question](http://stackoverflow.com/questions/16371129/chained-promises-not-passing-on-rejection) I linked above and maybe [this](http://stackoverflow.com/q/24662289/1048572) – Bergi Nov 23 '16 at 21:50

1 Answers1

2

The reason why someone might use Promise.js with Backbone could be to integrate easily with another lib but without the code, it's only speculations.

As mentionned by Bergi:

Because jQuery "promises" are not pure?

This was fixed in jQuery 3.


Backbone async functions like fetch uses jQuery's ajax in the background and it returns a jqXHR object.

The jQuery XMLHttpRequest (jqXHR) object returned by $.ajax() as of jQuery 1.5 is a superset of the browser's native XMLHttpRequest object.

[it] implement the Promise interface, giving them all the properties, methods, and behavior of a Promise (see Deferred object for more information).

Also, you could use Backbone async functions callbacks and options directly:

run: function () {
    if (this.initialAppViewMethod) {
        this.model.get('session').fetch({
            context: this,
            error: this.showLoginView,
            success: this.initMainView
        });
    }
},

It would be enough.

Community
  • 1
  • 1
Emile Bergeron
  • 17,074
  • 5
  • 83
  • 129
  • Thanks for that. At the moment I get an error within the _initMainView_ that this is unknow. initMainView: function() { this.appView = new sand.ApplicationView({ model: this.model }); – FaltFe Nov 23 '16 at 20:46
  • @FelixFaltin if you have a new problem which you can't solve, please ask a new question. – Emile Bergeron Nov 23 '16 at 20:48