Here is a possible implementation with async.js. If you have more than two properties, you will need to generate the "insertPropX" functions dynamically, but this should get you started.
var mssql = request.service.mssql;
var user = object.user;
function insertProp1(obj, cb) {
mssql.query("exec stored_procedure ?, ?", [user.userid, obj.prop1], {
success: function (res) {
var inserted = res[0];
if (!inserted) {
return cb('prop1 not inserted for userid: ' + user.userid);
}
// pass the ID and the object to the next
// function in the "waterfall" chain
cb(null, inserted.ID, obj);
}
});
}
function insertProp2(id, obj, cb) {
mssql.query("exec stored_procedure_2 ?, ?", [id, obj.prop2], {
success: function (res) {
var inserted = res[0];
if (!inserted) {
return cb('prop2 not inserted for id: ' + id);
}
// since this is the last function in
// the "waterfall" chain, we don't need
// to pass anything along
cb(null);
}
});
}
// for each object in the array...
async.each(object.array, function iterator (obj, cb) {
// insert properties in order, passing the
// insertion ID as an argument
async.waterfall([
insertProp1.bind(null, obj),
insertProp2
], cb);
}, function allDone (err) {
if (err) {
console.log(err);
}
// all done
});