mercredi 18 février 2015

Creating a class using Bluebird promises

I'm writing some code - a class with numerous methods I would traditionally use callbacks on and wanted to check that I am taking the correct approach.



// Callbacks
function Foo() { };
Foo.prototype.bar = function(cb) {
// Some async stuff
cb(err, res);
}

// Bluebird
function Foo() { };
Foo.prototype.bar = function() {
return new Promise(resolve, reject) {
// Some async stuff
resolve(res);
// etc...
});
}


Good to go? Or is there a cleaner approach? Most docs/tutorials are about promisfying existing callback based code and I am interested in creating a promise based class from sratch.


Aucun commentaire:

Enregistrer un commentaire