-1

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

3
  • You can't do that. You should use promises. Commented Feb 5, 2018 at 21:48
  • There are literally hundreds of questions and answers here on stack overflow about how to sequence (one after the other) asynchronous operation in node.js. You really ought to go find and read a whole bunch of those. It is a very common thing to have to learn when starting out in node.js. Commented Feb 5, 2018 at 23:11
  • either they are really hidden well or they don't serve what i'm looking for Commented Feb 6, 2018 at 12:42

1 Answer 1

1

Most of the file system calls have a synchronous equivalent. For example, sync version of fs.readdir is fs.readdirSync. Refer to the documentation: https://nodejs.org/api/fs.html

While you CAN call the synchronous versions of these functions, it is not really recommended. Refer to this post for more info on why: Node.js sync vs. async

Sign up to request clarification or add additional context in comments.

4 Comments

i'm forced to do them synchronously because like i mentioned above some of the expressions are dependent on the previous expressions in order to get the wanted result and i know about the equivalent versions of the fs.* and the fs.*sync but my fonctions are made i'm looking for a way to make them synchronously them too
"i'm forced to do them synchronously because like i mentioned above some of the expressions are dependent on the previous expressions". This is not true, you can still do async. Read about promises: developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/…
i have spended the last day trying to apply the promise on my code but couldn't do it an exemple would very much appreciated
brian glaz i already tried with promises but still no results the executions still happens after :/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.