I'm trying to develop a synchronous process in node.js, and I have implemented the async.series
method, but the way of I've implemented is not working as I expect. I expect the second function of the series works after the first function terminate (including the operation of the loop). As the s3.getObject
is an asynchronous function I'm a few lost how can I implement it correctly.
Resuming: I want to go to the second function after complete the loop and the functions inside the loop.
async.series([
function(cback) {
console.log("first function series...");
for (var i = 0; i < jsonUsuariosPerfil.users.length; i++) {
var usuario = jsonUsuariosPerfil.users[i];
var paramsUsers = {
Bucket: 's3.....br/.../users/' + usuario.user,
Key: 'profiles',
};
console.log("loop jsonUsuariosPerfil... " + usuario.user);
s3.getObject(paramsUsers, function(err, data) {
if (err) {
console.log("Error: " + err);
const message = "Error getting user -> " + err;
console.log(message);
callback(message);
} else {
var jsonPerfisUsuario = JSON.parse(data.Body);
var removido = false;
console.log("found object..." + JSON.stringify(data.Body))
for (var i = 0; i < jsonPerfisUsuario.profiles.length; i++) {
var perfil = jsonPerfisUsuario.profiles[i];
if (perfil.profile == id_grupo) {
jsonPerfisUsuario.profiles.splice(i, 1);
removido = true;
break;
}
}
if (removido) {
var paramsPut = {
Bucket: 's3.....br/.../users/' + usuario.user,
Key: 'profiles',
Body: JSON.stringify(jsonPerfisUsuario)
};
console.log("removido, salvando profiles");
s3.putObject(paramsPut, function(err2, data2) {
if (err2) {
console.log("error putting the file:" + err2);
var erro = {
status: "ERROR_SAVING_FILE"
};
generalCallback(null, erro);
} else {
console.log("salvo arquivo dos profiles do usuario");
}
});
}
}
});
}
cback();
},
function(cback) {
console.log("second function series...");
removerGrupoCompleto();
}
], function(err) {
if (err) {
var error = {
status: "ERROR"
};
generalCallback(null, error);
}
});
Log of what is happening:
pegou usuarios do perfil...
first function series...
loop jsonUsuariosPerfil... [email protected]
second function series..
Thanks a lot for all help.
async
pack. Thank you again! – pedro.olimpio Feb 7 at 12:11