Monday 30 November 2015

When is it expected to perform an operation sync/async?

I've been about a week trying to figure out how to do a callback that reads a file and returns the contents of the file to a variable that's available to the rest of the program. I figured that it's a "learning project" so I may as well get used to callbacks and later I'll delve into promises. I mean the exercise was more about recognizing when to do async vs sync.So Today I Learned that data inside a callback is unavailable to anything outside it... Ever. You can bring parameters into the callback function and do some work on it in there, but you're can't bring the result back out and put it, say, in a global variable. (if this is a wrong answer, please share.)Further I realized..."Oh... Duh... I don't necessarily want to be doing anything with the contents of the file before it's completely loaded, so I may as well use fs.readfile synchronously."What would be a good example of when it's best to do something sync/async?

Submitted November 30, 2015 at 07:44PM by monsto

No comments:

Post a Comment