3 ms·
You see how the caller has to be aware of how what he's calling is a generator. You're dragging generators all the way trough the call chain as soon as anything
by pyalot2 13y ago
You see how the caller has to be aware of how what he's calling is a generator. You're dragging generators all the way trough the call chain as soon as anything deep down in the call chain is a generator, and you'd want to use generators to acomplish asynchronicity.
Let's say you write some code A, which uses some other code B, which uses some other code C. C is also used by various other pieces of code like X, Y, Z, D, E, F, G, H, J, K etc. Now at some point you decide, well, you don't like callbacks in C, so you'll convert it to something asynchronous. You'll now just have to refactor A, B, D, E, F, G, H, J, K, X, Y and Z to make all their calls to C a generator iteration, and you'll have to convert any code that depends on A, B, D, E, F, G, H, J, K, X, Y or Z to be aware of that, any any code which uses those, to be aware of that.
- jeswin 13y agoSure, call sites of C will need to change, since C's signature changed. That's expected, since C doesn't even take a callback anymore. But callers of A,B,D,E etc don't need to change. A,B,D,E etc can still take a callback parameter and return C's result via the callback. MultiplyAndSquare = (a, b, cb) -> Multiply a, b, (err, product) -> Square product, (err, square) -> cb(null, square) #ignore errors for now. C = (a, b, cb) -> MultiplyAndSquare(a, b, cb) ABDEXY = (a, b, cb) -> #Calls C C(a, b, cb) callersOfABDEXY = (a, b) -> #Calls ABDEXY ABDEXY a, b, (err, result) -> Console.WriteLine(result) #Refactored C. Returns a promise instead. refactoredC = (a, b) -> (Q.async -> result = yield Q.nfcall(MultiplyAndSquare, a, b) return result)() #Refactored ABDEXY. refactoredABDEXY = (a, b, cb) -> #Change needed here, since C now returns a promise refactoredC(a, b).then (result) -> cb(null, result) #No need to refactor callersOfABDEXY