yosbelms / getjs

JavaScript library to express concurrency patterns.
30 stars 4 forks source link

Converting callbacks to channels or suspenders #3

Closed yosbelms closed 8 years ago

yosbelms commented 8 years ago

Node.js API uses callback approach for its async IO architecture, the next feature I'll add to Aryn is about addressing callback approach converting it to the Aryn way.

There is two proposals:

var fs = aryn.channelify(require('fs'))
var fileCh = fs.readFile('file.txt')

aryn.run(function*(){
    var content = yield receive(fileCh)
})
var fs = aryn.suspendify(require('fs'))
aryn.run(function*(){
    var content = yield receive(fs.readFile('file.txt'))
})

Both channelify and suspendify are supposed not to be used as function names in the final API, these are just for concepts explanation.

I like more the second approach for several reasons: 1 - It is decoupled to channels, but can be easily integrated with it passing content through a channel. 2 - It is integrated with the receive operation which is consistent with receive(promise) feature already implemented in Aryn. 3 - Allows natural error handling with try/catch, example:

aryn.run(function*(){
    try {
        var content = yield receive(fs.readFile('file.txt'))
    } catch (e){
        console.log(e)
    }
})

So, what do you think?

yosbelms commented 8 years ago

already addressed