Sync
opened this issue · comments
Deleted user commented
Hi, I've got an issue with an asynchronous function where i'm using sql requests.
function get_user_message_list(parameters, array, response)
{
var new_array = [];
for (var i in array) {
//console.log(i);
//console.log(array[i]);
var json = array[i];
json = get_json_message(parameters, json);
console.log(json);
new_array.push(json);
console.log(new_array);
}
console.log("NEW ARRAY :");
console.log(new_array);
response.writeHeader(200, {'Content-Type':'application/json'});
response.end(JSON.stringify(new_array));
}
function get_json_message(parameters, json)
{
console.log("JSON OBJECT :");
console.log(json);
var dataset = db.from(TABLES.USER).join(TABLES.MOVIE_LIST, {MLUserId: sql.URId}).join(TABLES.MOVIE, {MVId: sql.MLMovieId});
dataset.where('MLSeen={seen} AND MVSourceId={movie} AND MVSource={source} AND URId!={user}', {seen: 1, movie: json['content']['movie_id'], source: json['content']['movie_source'], user:parameters.FACEBOOK_ID}).all().then(function(users){
if (users) {
for (var j in users) {
json['content']['users_seen'].push(users[j].URId);
}
}
//console.log(json['content']['users_seen']);
dataset.where('MLSeen={seen} AND MVSourceId={movie} AND MVSource={source} AND URId!={user}', {seen: 0, movie: json['content']['movie_id'], source: json['content']['movie_source'], user:parameters.FACEBOOK_ID}).all().then(function(users){
if (users) {
for (var j in users) {
json['content']['users_not_seen'].push(users[j].URId);
}
}
console.log(json);
}, errorHandler);
}, errorHandler);
}
With this code, I want to wait the sql requests and then push the json to my array and after send the response to the client!
How can I achieve this ?
Doug Martin commented
Hi, the way I would do it would be to do something like the following.
//see http://pollenware.github.com/comb/index.html
var comb = require("comb"),
when = comb.when,
Promise = comb.Promise,
PromiseList = comb.PromiseList;
var get_json_message = function (parameters, json) {
var content = json.content;
var dataset = db
.from(TABLES.USER)
.join(TABLES.MOVIE_LIST, {MLUserId:sql.URId})
.join(TABLES.MOVIE, {MVId:sql.MLMovieId})
//create the dataset only once this will be more efficient
.where({MVSourceId:content.movie_id, MVSource:content.movie_source, URId:parameters.FACEBOOK_ID});
//create a promise to return
var ret = new comb.Promise();
when(
dataset.where({MLSeen:0}).selectMap("URId"),
dataset.where({MLSeen:1}).selectMap("URId")
).then(function (res) {
content.users_seen = content.users_seen.concat(res[0]);
content.users_not_seen = content.users_not_seen.concat(res[0]);
ret.callback(json);
}, ret); //notice how I pass ret back into the .then call this acts as an errback
};
var get_user_message_list = function (parameters, array, response) {
new PromiseList(array.map(function (json) {
return get_json_message(parameters, json);
})).then(function () {
response.writeHeader(200, {'Content-Type':'application/json'});
//when working with objects it is all passby reference so copying the values over to the new array
//is pointless unless you return a new object
response.end(JSON.stringify(array));
}, errorHandler);
}
Deleted user commented
Thank you for you help !! I'm a beginner in the world of JavaScript and async function so it's really hard for me :/