I'm trying to run synchronous execution in node js is there any way to do that other than using events and Async because those didn't work for me?
for (i=1; i < response.payload.parts.length; i++) {
if(response.payload.parts[i]!==undefined && response.payload.parts[i].filename!==undefined) {
console.log("5")
myEmitter.emit('trah', auth, 'me', response.id, response.payload.parts[i].body.attachmentId, response.payload.parts[i].filename,response.payload.parts[i].mimeType);
}
}
var ul = fs.readdir('content/'+response.id,(err) => {
if(err) { console.log(err) }
})
console.log("9")
if(ul!==undefined) {
console.log('mahiech lehné')
for (var i=0; i<ul.length; i++) {
if(mime.getType(ul[i]).substring(0,5)==='image') {
console.log("10")
// console.log(te)
te+='<a href="http://localhost:3000/content/?id='+response.id+'&atch='+ul[i]+ '"><img border="0" src="http://localhost:3000/content/?id='+response.id+'&atch='+ul[i]+'" width="100" height="100"></a>'
}
console('11----'+i)
fs.writeFile(__dirname+'/test.txt',te)
}
}
}
fs.writeFile(__dirname+'/test1.txt',te)
console.log("12")
res.end(buildres(te))
This my 'trah' event
myEmitter.on('trah', (auth,userId,messageId,id,name,type) => {
gmail.users.messages.attachments.get({
auth: auth,
userId: userId,
messageId: messageId,
id: id},
function (err, res) {
if (err) {
console.log('The API returned an error: ' + err);
return;
}
if (!res) {
console.log('no resposnse found')
}
if (res===undefined) {
console.log('No messages found.');
}
if (res) {
console.log("6")
if(type.substring(0, 5)==='image'){
res.data = res.data.replace(/_/g, "/");
res.data = res.data.replace(/-/g, "+");
console.log("7")
fs.writeFile(__dirname + '/content/' + messageId + '/'+name,res.data,'base64',(err)=>{console.log(err)})
}
else {
console.log("8")
fs.writeFile(__dirname + '/content/' + messageId + '/' + name, utf8.decode(base64.decode(res.data)),(err)=>{console.log(err)})
}
}
})
});
this is the execution order i get since my execution is depentdent on each ordered statement i can't get it to work right 1 2 3 4 5 9 12 6 8