1

I know that one can synchronously read a file in NodeJS like this:

var fs = require('fs');
var content = fs.readFileSync('myfilename');
console.log(content);

I am instead interested in being able to read the contents from a stream into a string synchronously. Thoughts?

1
  • You can't, and you shouldn't Commented Jun 16, 2015 at 22:38

1 Answer 1

1

Streams in node.js are not synchronous - they are event driven. They just aren't synchronous. So, you can't get their results into a string synchronously.

If you have no choice but to use a stream, then you have no choice but to deal with the contents of the stream asynchronously.

If you can change the source of the data to a synchronous source such as the fs.readFileSync() that you show, then you can do that (though generally not recommended for a multi-user server process).

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

Comments

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.