Particularly i want to promisify model.fetch method, so when I'm creating model I promisifying Backbone
function (_, Backbone, Promise) {
Backbone = Promise.promisifyAll(Backbone);
var Diagram = Backbone.Model.extend({...});
}
But later I'm trying to use fetchAsync, on my diagram model and nothing happens.
diagram.fetchAsync()
.then(function() {
console.log('success');
},
function() {
console.log('err');
})
.catch(function() {
console.error('fetch failed');
});
in console this promise looks like this
_bitField: 0
_fulfillmentHandler0: undefined
_promise0: undefined
_receiver0: undefined
_rejectionHandler0: undefined
__proto__: Object
I think that fetchAsync calls non promisifyed versions of sync and ajax calls that do not return resolve in this promise. I'm new to promises so sorry if I wrote something silly.
On promisifying backbone I found very little info in google, just several packages on npm, but I don't want to include extra packages, especcially if they are not popular.
As @BenjaminGruenbaum says in the comments
fetch
already returns a promise in backbone, no need to promisify it.
promisify
only works on functions that have the node function convention
however fetch returns jqXHR and I want Bluebird promise, so I need to use Bluebird.resolve() and that's it.
So I ended up with this
Promise.resolve(diagram.fetch())
.then(function (res) {
console.log('success');
})
.catch(() => {
console.error('fetch failed');
});