I thought I'll create a ticket to discuss various topics about IO model for Gravity.
In async/await ticket @parro-it mentioned that he's working on libuv bindings to Gravity, which is cool, though I wonder if event loop should be build-in rather then come in a form of binding.
I think the main answer that IO model should give is the answer to the question:
"how do you get the data into a variable?"
// blocking. Execution won't continue until data is ready.
var data = File.read(path);
// async with callbacks (node, browsers)
File.read(path, func(error, data) {
// ...
});
// async with Promises (node, browsers)
File.read(path).then(func(data) {
// ...
});
// async/await
var data = await File.read(path);
// streaming
var data = [];
Stream.read(path).on('data', func(chunk) {
data.push(chunk);
}).on('end', func() {
data = data.join('');
});
Of course, these examples are not state-of-the art ones, but short enough to show different models, you are totally welcome to propose any other ideas.
One thing I wanted to discuss is do we really need Promises as underlying pattern for async/await or we can somehow make Fibers better instead so that they support this use-case? Maybe Fiber can save last returned result into a variable that can be accessed later?
var fiber = Fiber.create({
// or last Fiber.yield(), you get the idea :)
return File.read(path);
});
var data = await fiber;
I'm not proposing any of these as one and only way of doing things, I just want discussion to get started :)
I thought I'll create a ticket to discuss various topics about IO model for Gravity.
Of course, these examples are not state-of-the art ones, but short enough to show different models, you are totally welcome to propose any other ideas.
One thing I wanted to discuss is do we really need Promises as underlying pattern for async/await or we can somehow make Fibers better instead so that they support this use-case? Maybe Fiber can save last returned result into a variable that can be accessed later?
I'm not proposing any of these as one and only way of doing things, I just want discussion to get started :)