> Basically callbacks and coroutines don't play well together.
Can you elaborate on this? I thought the opposite: coroutines play very well with callbacks. Callback libraries force you to write programs in continuation passing style. Coroutines let you write asynchronous programs in normal style. Lets assume we have an asynchronous readAsync(callback) function that reads a number and calls the callback with the input when it's ready. Now you're writing your program like this:
readAsync(function(x){
readAsync(function(y){
write(x+y)
})
})
What you want to write is this:
x = read() // may block this coroutine, but *not* the entire program
y = read()
write(x+y)
This can be achieved with something like:
function read(){
coro = getCurrentCoroutine()
var result
callback = function(x){ result = x; coro.resume() }
readAsync(callback)
yield // stops the current coroutine until it is resumed
return result
}
It seems to me that this is a general way to adapt any callback style library to coroutines.