How to set async/await inside a for loop for bulkwrite? How to set async/await inside a for loop for bulkwrite? mongoose mongoose

How to set async/await inside a for loop for bulkwrite?


I personally believe you are over complicating your current solution, but without seeing actual code, that is hard to say if this is the case.

For one, as far as I can see, your code doesn't really have to wait in the for loop, it seems your bulk writes are fine if they are fired away, and just waited for as the result of your update.

Another complication (as far as I can see from the code), is the double check to see if you sent all items to your server. If your source data comes as an array, or something that can be transformed in place, there is no need for neither the for loop as the double check.

The code supplied does make the assumption that your updates do not influence the coming updates (in both versions, nl, the performBulkUpdate and performSyncBulkUpdate)

This code is used to split your source array, and create the bulk items, then executes the action, and saves the promise, afterwards the promise.all( results ) is awaited.

// as long as i is smaller than the length of the arraywhile (i < content.length) {  // take the amount of items, perform transformation and save the promise  let itemsToWrite = content.slice(i, i + bulkSize ).map( transformation );  results.push( action( itemsToWrite ) );  // increase the index with the bulkSize  i += itemsToWrite.length;}

Of course, if for some reason, you have to wait till each write operation has finished, then you have to change the approach a bit, by awaiting each single write operation, either like this:

// as long as i is smaller than the length of the arraywhile (i < content.length) {  let itemsToWrite = content.slice(i, i + bulkSize ).map( transformation );  results.push( await action( itemsToWrite ) );  // increase the index with the bulkSize  i += itemsToWrite.length;}

below is a snippet demonstrating both approaches

// source array that must be bulkupdatedconst source = [10, 5, 6, 7, 9, 15, 33, 47, 42, 63, 77, 99];// some random action that returns a promiseconst bulkWrite = (arr) => {  return new Promise( (resolve) => {    setTimeout( () =>       resolve( arr.map( i => { return { id: i && i.sourceId }; } ) )    , 100 );  } );};// concat arrays togetherconst flatten = (arr) => arr.reduce( (current, item) => current.concat( item ), [] );// dummy transformation suiting the write operationconst transformItem = (item) => ({ sourceId: item });// the bulk update (default bulkSize = 3)const performBulkChange = async ( content, action, transformation, bulkSize = 3 ) => {  // the array containing the promises  const results = [];  let i = 0;    // as long as i is smaller than the length of the array  while (i < content.length) {    // take the amount of items, perform transformation and save the promise    let itemsToWrite = content.slice(i, i + bulkSize ).map( transformation );    results.push( action( itemsToWrite ) );    // increase the index with the bulkSize    i += itemsToWrite.length;  }  return await Promise.all( results );};const performSyncBulkChange = async ( content, action, transformation, bulkSize = 3 ) => {  // all results  const results = [];  let i = 0;    // as long as i is smaller than the length of the array  while (i < content.length) {    let itemsToWrite = content.slice(i, i + bulkSize ).map( transformation );    results.push( await action( itemsToWrite ) );    // increase the index with the bulkSize    i += itemsToWrite.length;  }  return results;};console.log( 'starting' );// slower, takes at least 100 ms per bulkperformSyncBulkChange( source, bulkWrite, transformItem, 5 )  .then( flatten )  .then( result => console.log( result ) );// bulk change will be faster, it doesn't waitperformBulkChange( source, bulkWrite, transformItem )  .then( flatten )  .then( result => console.log( result ) );