Skip to main content
added 579 characters in body
Source Link
Prisoner
  • 50.9k
  • 7
  • 59
  • 113

There are several problems in both the question and what you're trying to do. You may want to either clarify the question or clarify what you really want to do.

The short answer to your question: No. A for loop is not, itself, asynchronous.

A slightly longer answer: It isn't clear you need the loop itself to be async, but you may just want the contents to be async. So instead of calling readFileSync you may want to use readFile which takes a callback. In this callback, you can append to the file. This will "fire off" 1000 read files, which will call their callback when ready.

But there is still a problem. If you read the 1000 files asynchronously, and save them to the same file as you read them in, you won't get the file contents in any order and may even have file content interleaving. I can't imagine why you actually want to do this.

Update: Your updated question is still a little vague - if you need to wait until everything is done to send the code 200 reply... then you're going to have to wait until all the files are appended. There really isn't any other solution.

If, however, you can return code 200 immediately to indicate you're working on the file appends, and then do the appending in the background, you can execute the loop as part of an asynchronous task. You can do this inside a process.nextTick() call or in a promise or in any other way that makes sense to your flow.

There are several problems in both the question and what you're trying to do. You may want to either clarify the question or clarify what you really want to do.

The short answer to your question: No. A for loop is not, itself, asynchronous.

A slightly longer answer: It isn't clear you need the loop itself to be async, but you may just want the contents to be async. So instead of calling readFileSync you may want to use readFile which takes a callback. In this callback, you can append to the file. This will "fire off" 1000 read files, which will call their callback when ready.

But there is still a problem. If you read the 1000 files asynchronously, and save them to the same file as you read them in, you won't get the file contents in any order and may even have file content interleaving. I can't imagine why you actually want to do this.

There are several problems in both the question and what you're trying to do. You may want to either clarify the question or clarify what you really want to do.

The short answer to your question: No. A for loop is not, itself, asynchronous.

A slightly longer answer: It isn't clear you need the loop itself to be async, but you may just want the contents to be async. So instead of calling readFileSync you may want to use readFile which takes a callback. In this callback, you can append to the file. This will "fire off" 1000 read files, which will call their callback when ready.

But there is still a problem. If you read the 1000 files asynchronously, and save them to the same file as you read them in, you won't get the file contents in any order and may even have file content interleaving. I can't imagine why you actually want to do this.

Update: Your updated question is still a little vague - if you need to wait until everything is done to send the code 200 reply... then you're going to have to wait until all the files are appended. There really isn't any other solution.

If, however, you can return code 200 immediately to indicate you're working on the file appends, and then do the appending in the background, you can execute the loop as part of an asynchronous task. You can do this inside a process.nextTick() call or in a promise or in any other way that makes sense to your flow.

Source Link
Prisoner
  • 50.9k
  • 7
  • 59
  • 113

There are several problems in both the question and what you're trying to do. You may want to either clarify the question or clarify what you really want to do.

The short answer to your question: No. A for loop is not, itself, asynchronous.

A slightly longer answer: It isn't clear you need the loop itself to be async, but you may just want the contents to be async. So instead of calling readFileSync you may want to use readFile which takes a callback. In this callback, you can append to the file. This will "fire off" 1000 read files, which will call their callback when ready.

But there is still a problem. If you read the 1000 files asynchronously, and save them to the same file as you read them in, you won't get the file contents in any order and may even have file content interleaving. I can't imagine why you actually want to do this.