Q.all([x(), y()]).then(function(results) {
// Do some stuff with results, which is an enumerable collection containing
// the in-order results of the promises passed in.
});
Q.all([x(), y()]).then(function(results) {
// Do some stuff with results, which is an enumerable collection containing
// the in-order results of the promises passed in.
});
// Upgrade x into a promise returning function
function promise_x(/*x params...*/) {
return Q.fcall(x(/*x params...*/));
}
// Upgrade x into a promise with a deferred
// Useful if you have some standard error handling specific to x-like functions
function defer_x(/*x params...*/) {
var deferred = Q.defer();
// If x is asynch, assume it takes a callback function with params of (error, result)
x(/*x params...*/), function (error, result) {
if (error) {
deferred.reject(new Error(error));
} else {
// If you have some processing to do with the result of x (and not y), you can do
// it here and return the modified result instead
deferred.resolve(result);
}
});
return deferred.promise;
}
// Upgrade x into a promise returning function
function promise_x(/*x params...*/) {
return Q.fcall(x(/*x params...*/));
}
// Upgrade x into a promise with a deferred
// Useful if you have some standard error handling specific to x-like functions
function defer_x(/*x params...*/) {
var deferred = Q.defer();
// If x is asynch, assume it takes a callback function with params of (error, result)
x(/*x params...*/), function (error, result) {
if (error) {
deferred.reject(new Error(error));
} else {
// If you have some processing to do with the result of x (and not y), you can do
// it here and return the modified result instead
deferred.resolve(result);
}
});
return deferred.promise;
}
MyOverallFunction() {
Q.all([x(), y()]).then(function(results) {
// Do some stuff with results, which is an enumerable collection containing
// the in-order results of the promises passed in.
var combinedResult = synchronousFunction(result[0], result[1]);
anotherAsyncPromiseFn(combinedResult)
.then(function(result)) {
return result;
});
}).then(function(result) {
// Here, result is the return value of anotherAsyncPromiseFn
}).error(function(err) {
// This is the cool part. Any error from x, y, synchronousFunction, or
// anotherAsyncPromiseFn will propagate to here. If you don't want to
// handle it, just re-throw it (after logging it, if you prefer).
});
}
MyOverallFunction() {
Q.all([x(), y()]).then(function(results) {
// Do some stuff with results, which is an enumerable collection containing
// the in-order results of the promises passed in.
var combinedResult = synchronousFunction(result[0], result[1]);
anotherAsyncPromiseFn(combinedResult)
.then(function(result)) {
return result;
});
}).then(function(result) {
// Here, result is the return value of anotherAsyncPromiseFn
}).error(function(err) {
// This is the cool part. Any error from x, y, synchronousFunction, or
// anotherAsyncPromiseFn will propagate to here. If you don't want to
// handle it, just re-throw it (after logging it, if you prefer).
});
}