I recently started using the q promise javascript library for my Node.js application. In my code I have a conditional that determines if I should execute 1 or 3 promise methods. I then want to execute the array of promise-returning methods and perform additional processing on the results.
Question: How do I use the results inside and outside .spread(...)
, so I can pass the result to other methods?
Some background: I am building a REST API, where depending on the values of the POSTed JSON body, I have to insert records into the DB using Sequelize first, get a response (so I know the auto-generated primary key), then insert records in other tables using that primary key.
var promiseMethods;
var someParam1, someParam2, someParam3 = 'Hello World';
if (someCondition == true) {
promiseMethods = [promiseMethod1(someParam1)];
} else {
promiseMethods = [
promiseMethod1(someParam1),
promiseMethod2(someParam2),
promiseMethod3(someParam3)];
}
Q.all(promiseMethods)
.spread(function(promseResult1,
promiseResult2,
promiseResult3) {
var model = {
result: promiseResult1
};
//Other code removed for brevity.
return DatabasePromise.save(model)
});
I want to be able to then do something like the following hypothetical code.
var result = add(promiseResult1)
var result2 = subtract(promiseResult2)
var result3 = multiply(promiseResult3)
Essentially I want to execute the first 1 or 3 promise-returning methods in parallel, then be able to use the promise result(s) in multiple times thereafter.
Here is a better idea of what I want the code to do. The example here is that I have a product table, with users who are associated with that product. I want to insert data into these two tables (as well as a third table).
var _ = require('lodash');
function insert(request, reply) {
var response;
var db = getDBConnection();
var step1Promises;
if (property == true) {
step1Promises = [
CommonModule.promiseMethodForProductUsers(db, userUUIDs)
];
} else {
step1Promises = [
CommonModule.promiseMethodForProductUsers(db, userUUIDs),
CommonModule.promiseMethod2(payload.data1, payload.data2),
CommonModule.promiseMethod3(db, payload.data3)
];
}
var step1 = Q.all(step1Promises);
var resolvedProductUsers;
var internalIDs;
var productPromise = step1.spread(function(productUsers, promiseMethod2, promiseMethod3) {
resolvedProductUsers = productUsers;
var currentUser = users[0].uid;
internalIDs = Q(_.pluck(resolvedProductUsers, 'uid'));
var product = db.Product.build({
product_name: productName,
last_edit_user_id: currentUser
});
return CommonModule.createProduct(db, product);
});
// When running, internalIDs is undefined.
var step2 = Q.all([
CommonModule.buildProductUsersModel(db, internalIDs, productPromise.tid),
CommonModule.buildOtherTableModel(db, productPromise.pid, currentUser.Id, otherData)
]);
step2.spread(function(usersToInsert, otherTableDataToInsert) {
CommonModule.saveProductUsers(db, usersToInsert);
CommonModule.saveOtherTableData(db, otherTableDataToInsert);
});
// The response must be a promise, otherwise this insert method will finish
// and return an empty result.
response = productPromise;
return reply(response);
}