Re:Node.js guys discussing async programming style
by Kilimanjaro on Sunday February 21, 2010 @09:23AM
(#1140577)
For example, usinging XHR model:
file = new File.Reader()
file.onReady = printfile
file.onError = showerror
file.onProgress = showprogress
file.read('myfile.txt')
Not restricted to callback and errback only, more events may be available according to the process being executed, keeping JS as natural as possible.Hacks can be added on top to handle parallel or chainable process.
One last thing, make the process asynchronous by default and set a property to false to make it synchronous, no need to force a last parameter to a function.
file.async = false
file.read('myfile.txt')
Every process/function that deals with i/o,db,etc should provide at least two events onReady and onError (drop the 'callback' and 'errback' aberrations) and leave JS as pure as it is right now.
Other events may be used like onProgress, onLoad, etc.