Join GitHub today
add a macro to ease the use node.js callbacks? #56
Was just playing with a little example of walking file directories asyncronously with node.js and it struck me lispyscript might be well positioned to simplify/hide/eliminate the use of callbacks...
I actually tried it out with a little macro and was delighted with how easy lispyscript made it (i'd been playing with generators recently and this was remarkably simple by comparison!!).
Here's my original function in lispyscript using callbacks:
Here's the same code with a little "await" macro (which I show further down):
And here's the little "await" macro:
As I mentioned above - I was delighted the above macro was so short and actually worked perfectly on the first try!!
I was trying to think if the same macro could work with both callback taking functions and promise-returning functions without the programmer having to use a different macro.
I couldn't think of any way - please others comment if somebody sees a way.
As it is I could easily see their being an "awaitp" for "await promise" as a sister to the above?
Also I obviously have ignored errors in the above I need to correct that. I thought I would have the macro include code to throw the received "err" parameter if it gets one.
But maybe there should be a way to handle the err parameter yourself if you want to... The ideal syntax to me would look like destructuring assignment:
So this if you want to handle the err yourself:
This if you want it to throw if there's an error:
Would that be doable I wonder?
(still learning the finer points of macros - seems to me the above implies in my macro I need some logic distinguishing what form I've gotten as the first "result" argument to the macro. I don't know how to do that ATM).