jeudi 26 mars 2015

How to use Q.denodeify while converting Node async functions to promises?

Following code works just fine:



var Q = require('q');

function async_f1(callback) {
var deferred = Q.defer();
setTimeout(function() {
deferred.resolve("Async function 1...");
}, 1000);
return deferred.promise;
}

function async_f2(callback) {
var deferred = Q.defer();
setTimeout(function() {
deferred.resolve("Async function 2!...");
}, 1000);
return deferred.promise;
}

function async_f3(callback) {
var deferred = Q.defer();
setTimeout(function() {
deferred.resolve("Second async function 3!...");
}, 1000);
return deferred.promise;
}

Q.spread([async_f1(), async_f2(), async_f3()], function(a, b, c) {
console.log(a, b, c);
})


But I really don't think it's the best way to convert async functions into promises. There are a lot of repeatings of defer stuff. I've read some about Q.denodeify feature but I was unable to implement it.


Could you show me how it is done in this particular case?


Thank you


Aucun commentaire:

Enregistrer un commentaire