Node.js 如何在NodeJS中运行同步执行?

Node.js 如何在NodeJS中运行同步执行?,node.js,Node.js,我正在尝试在node js中运行同步执行,除了使用事件和异步之外,还有什么其他方法可以做到这一点,因为它们对我不起作用吗 for (i=1; i < response.payload.parts.length; i++) { if(response.payload.parts[i]!==undefined && response.payload.parts[i].filename!==undefined) { console.log("5")

我正在尝试在node js中运行同步执行,除了使用事件和异步之外,还有什么其他方法可以做到这一点,因为它们对我不起作用吗

 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))
这是我得到的执行顺序,因为我的执行依赖于每个有序语句,我无法让它正常工作 1. 2. 3. 4. 5. 9 12 6.
8

大多数文件系统调用都有一个同步等价物。例如,
fs.readdir
的同步版本是
fs.readdirSync
。请参阅文档:


虽然您可以调用这些函数的同步版本,但实际上并不推荐这样做。有关原因的更多信息,请参阅本文:

你不能这样做。你应该使用承诺。这里有成百上千的关于如何在node.js中排序(一个接一个)异步操作的问题和答案。你真的应该去寻找和阅读一大堆这些。在node.js中开始时,这是一件非常常见的事情。要么它们真的隐藏得很好,要么它们不符合我的要求。我被迫同步执行它们,因为正如我上面提到的,为了得到想要的结果,一些表达式依赖于前面的表达式,我知道它们的等价物fs.*和fs.*同步的版本,但我的功能是同步的,我正在寻找一种方法使它们也同步“我被迫同步,因为正如我上面提到的,一些表达式依赖于前面的表达式”。事实并非如此,您仍然可以执行异步。阅读关于承诺的文章:我花了最后一天的时间试图在我的代码中应用承诺,但无法实现。一个例子将非常感谢Brian glaz我已经尝试过承诺,但仍然没有结果执行后仍然发生:/
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)})
                }
            }
        })
});