如何在循环中处理异步Node.js.

时间:2017-07-21 15:42:58

标签: javascript node.js loops synchronization

我有这样一个循环:

var i,j,temparray,chunk = 200;
for (i=0,j=document.mainarray.length; i<j; i+=chunk) {
  temparray = document.mainarray.slice(i,i+chunk);

  var docs =  collection.find({ id: { "$in": temparray}}).toArray();

  docs.then(function(singleDoc)
  {
    if(singleDoc)
    {
      console.log("single doc length : " + singleDoc.length);
      var t;
      for(t = 0, len = singleDoc.length; t < len;t++)
      {
        fs.appendFile("C:/Users/x/Desktop/names.txt", singleDoc[t].name + "\n", function(err) {
          if(err) {
            return console.log(err);
          }
        });
      }
    }
  });
}

循环迭代两次。在第一次迭代中,它获得200个元素,其次,它获得130个元素。当我打开.txt文件时,我只看到130个名字。我想因为Node.js的异步性质,只处理数组的第二部分。我该怎么做才能处理数组的所有部分?提前谢谢。

编辑:我终于将代码改为:

var generalArr = [];
var i,j,temparray,chunk = 200;
for (i=0,j=document.mainarray.length; i<j; i+=chunk) {
    temparray = document.mainarray.slice(i,i+chunk);

generalArr.push(temparray);


} 

async.each(generalArr, function(item, callback)
{

  var docs =  collection.find({ id: { "$in": item}}).toArray();

   docs.then(function(singleDoc)
  {
    if(singleDoc)
    {
      console.log("single doc length : " + singleDoc.length);
              var t;
        for(t = 0, len = singleDoc.length; t < len;t++)
        {    
           fs.appendFile("C:/Users/x/Desktop/names.txt", singleDoc[t].name + "\n", function(err) {
          if(err) {
          return console.log(err);
          }
        });
        }
    }


  });

  callback(null);
})

当我改变这一行时:

var docs =  collection.find({ id: { "$in": item}}).toArray();

到这一行:

var docs =  collection.find({ id: { "$in": item}}).project({ name: 1 }).toArray();

它有效,我能够打印所有名字。我猜没有.project()时我的内存存在问题。如何在不使用项目的情况下完成这项工作?我应该更改一些内存限制吗?提前谢谢。

6 个答案:

答案 0 :(得分:3)

我认为你的代码是不必要的复杂,并且与内存计算相比,在循环中附加文件非常昂贵。更好的方法是只写一次文件。

var i, j, temparray, chunk = 200;
for (i = 0, j = document.mainarray.length; i < j; i += chunk) {
  temparray = document.mainarray.slice(i, i + chunk);
  generalArr.push(temparray);
}
const queryPromises = [];
generalArr.forEach((item, index) => {
  queryPromises.push(collection.find({ id: { "$in": item } }).toArray());
});
let stringToWrite = '';
Promise.all(queryPromises).then((result) => {
  result.forEach((item) => {
    item.forEach((element) => {
      //create a single string which you want to write
      stringToWrite = stringToWrite + "\n" + element.name;
    });
  });
  fs.appendFile("C:/Users/x/Desktop/names.txt", stringToWrite, function (err) {
    if (err) {
      return console.log(err);
    } else {
      // call your callback or return
    }
  });
});

在上面的代码中,我执行以下操作。

  1. 等待所有数据库查询完成
  2. 让我们遍历此列表并创建一个我们需要写入文件的字符串
  3. 写入文件

答案 1 :(得分:2)

一旦你进入异步,就无法返回 - 所有代码都需要异步。在节点8中,您可以使用asyncawait关键字处理此问题。在旧版本中,您可以使用Promise - async / await无论如何都只是语法糖。

但是,节点中的大多数API都早于Promise,因此它们使用回调。有一个promisify函数可以将回调函数更新为promises。

有两种方法可以解决这个问题,您可以让所有异步操作同时发生,或者您可以将它们一个接一个地链接起来(保留顺序但需要更长时间)。

因此,collection.find是异步的,它需要一个回调函数或返回一个Promise。我将假设您使用的API执行后者,但您的问题可能是前者(在这种情况下查找promisify)。

var findPromise =  collection.find({ id: { "$in": item}});

现在,此时findPromise包含正在运行的find操作。我们说这是承诺 解析(成功完成)或拒绝(抛出错误)。我们希望在完成后将要执行的操作排队,然后我们使用then执行此操作:

// The result of collection.find is the collection of matches
findPromise.then(function(docs) {
    // Any code we run here happens asynchronously
});

// Code here will run first

在承诺内部我们可以返回进一步的承诺(允许它们被链接 - 完成一个异步,然后完成下一个,然后一旦完成就解雇最终解决方案)或使用Promise.all让它们全部并行发生并在完成后解决:

var p = new Promise(function(resolve, reject) {
    var findPromise =  collection.find({ id: { "$in": item}});
    findPromise.then(function(docs) {
        var singleDocNames = [];
        for(var i = 0; i < docs.length; i++) {
            var singleDoc = docs[i];
            if(!singleDoc)
                 continue;

            for(var t = 0; t < singleDoc.length; t++)
                singleDocNames.push(singleDoc[t].name);
        }

        // Resolve the outer promise with the final result
        resolve(singleDocNames);
    });
});

// When the promise finishes log it to the console
p.then(console.log);

// Code inline here will fire before the promise

使用async / await在节点8中 更容易:

async function p() {
    // Await puts the rest of this function in the .then() of the promise
    const docs = await collection.find({ id: { "$in": item}});

    const singleDocNames = [];
    for(var i = 0; i < docs.length; i++) {
        // ... synchronous code unchanged ...
    }

    // Resolve the outer promise with the final result
    return singleDocNames;
});

// async functions can be treated like promises
p().then(console.log);

如果你需要异步地将结果写入文本文件,有几种方法可以做到 - 你可以等到最后并写下所有这些,或者在每次查找后链接一个承诺来写它们,尽管我发现并行IO操作往往更容易出现死锁。

答案 2 :(得分:1)

上面的代码有关于异步控制流的多个问题。可能存在类似的代码,但仅限于在所有异步操作中使用ES7 async / await运算符的情况。

当然,您可以通过承诺序列轻松实现解决方案。解决方案:

let flowPromise = Promise.resolve();

const chunk = 200;
for (let i=0,j=document.mainarray.length; i<j; i+=chunk) {
    flowPromise = flowPromise.then(() => {
        const temparray = document.mainarray.slice(i,i+chunk);
        const docs =  collection.find({ id: { "$in": temparray}}).toArray();
        return docs.then((singleDoc) => {
            let innerFlowPromise = Promise.resolve();
            if(singleDoc) {
                console.log("single doc length : " + singleDoc.length);
                for(let t = 0, len = singleDoc.length; t < len;t++) {
                    innerFlowPromise = innerFlowPromise.then(() => new Promise((resolve, reject) =>
                        fs.appendFile(
                            "C:/Users/x/Desktop/names.txt", singleDoc[t].name + "\n",
                            err => (err ? reject(err) : resolve())
                        )
                    ));
                }
            }
            return innerFlowPromise;
        }
    });
}

flowPromise.then(() => {
    console.log('Done');
}).catch((err) => {
    console.log('Error: ', err);
})

当使用类似异步的控制流时,基于Promise,始终记住每个循环和函数调用序列都不会暂停执行,直到完成异步操作,因此手动包含所有then序列。或者使用async / await语法。

答案 3 :(得分:1)

您使用的是哪个版本的nodejs?您应该使用内置于较新版本nodejs的本机async/await支持(无需库)。另请注意,fs.appendFile是异步的,因此您需要使用像promisify这样的库将回调转换为承诺,或者只使用appendFileSync并遭受阻塞IO(但可能没问题)对你来说,取决于用例。)

async function(){
    ...
    for(var item of generalArr) {
      var singleDoc = await collection.find({ id: { "$in": item}}).toArray();
      // if(singleDoc) { this won't do anything, since collection.find will always return something even if its just an empty array
      console.log("single doc length : " + singleDoc.length);
      var t;
      for(t = 0, len = singleDoc.length; t < len;t++){    
          fs.appendFileSync("C:/Users/x/Desktop/names.txt", singleDoc[t].name + "\n");
       }

    };
}    

答案 4 :(得分:0)

var docs =  collection.find({ id: { "$in": document.mainarray}}), // returns a cursor
  doc,
  names = [],
  toInsert;

function saveToFile(cb) {
  toInsert = names.splice(0,100);
  if(!toInsert.length) return cb();
  fs.appendFile("C:/Users/x/Desktop/names.txt", toInsert.join("\n"), cb);
}

(function process() {
  if(docs.hasNext()) {
    doc = docs.next();

    doc.forEach(function(d) {
      names.push(d.name);
    });

    if(names.length === 100) {
      // save when we have 100 names in memory and clear the memory
      saveToFile(function(err) {
        process();
      });
    } else {
       process();
    }
  } else {
    saveToFile(function(){
      console.log('All done');
    });
  }
}()); // invoke the function

答案 5 :(得分:0)

如果您无法使用核心模块和基本nodejs解决您的问题,则很可能缺乏对工作原理或对库的知识不足的理解(在本例中为FileSystem模块)。

如果没有第三方图书馆等,您可以在这里解决问题。

'use strict';

const
    fs = require('fs');

let chunk = 200;

// How many rounds of array chunking we expect 
let rounds = Math.ceil(mainArray.length/chunk);
// copy to temp (for the counter)
let tempRounds = rounds;
// set file name
let filePath = './names.txt'


// Open writable Stream
let myFileStream = fs.createWriteStream(filePath);


// from round: 0-${rounds}
for (let i = 0; i < rounds; i++) {
    // assume array has ${chunk} elements left in this round
    let tempChunk = chunk;
    // if ${chunk} is to big i.e. i=3 -> chunk = 600 , but mainArray.length = 512
    // This way we adjust the last round for "the leftovers"
    if (mainArray.length < i*chunk) tempChunk = Math.abs(mainArray.length - i*chunk);
    // slice it for this round
    let tempArray = mainArray.slice(i*chunk, i*chunk + tempChunk);
    // get stuff from DB
    let docs =  collection.find({ id: { "$in": tempArray}}).toArray();

    docs.then(function(singleDoc){
        // for each name in the doc
        for (let j = 0; j < singleDoc.length; j++) {
            // write to stream
            myFileStream.write(singleDoc[t].name + "\n");
        }
        // declare round done (reduce tempRounds) and check if it hits 0
        if (!--tempRounds) {
            // if all rounds are done, end the stream
            myFileStream.end();
            // BAM! you done
            console.log("Done")
        }
    });
}

关键是要使用fs.WritableStreams :) link here to docs

相关问题