Felix Faltin Felix Faltin - 1 year ago 86
Javascript Question

Backbone with Promise.js or pure jQuery to resolve promises

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) {

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?

Answer Source

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) {
            context: this,
            error: this.showLoginView,
            success: this.initMainView

It would be enough.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download