Hi, I hope I'm in the right place.
I was looking at ways to structure asynchronous code. I like what async/await does but it still requires knowing a lot about promises to wield properly. I dug into async/await implementations and found that the promises seemed unnecessary and kinda superfluous. It's fairly easy to create something simpler with plain old callbacks: https://github.com/bessiambre/casync (there is also the somewhat similar https://github.com/tj/co, but this one seems to have evolved into something quite complex).
Now without language level syntax sugar, it doesn't look as simple as it could and it makes it difficult to write generator functions in the same style since using yield to 'await' makes it confusing to use it to yield
a value also.
I would find a simplified, callback based async/await (sync/wait?) very useful when working with callback based code.
As a library it looks like:
const {casync} = require('casync');
let addTitleToReadme=casync(function*(prepend,done,next){
let data = yield fs.readFile('README.md',next);
return prepend+"\n"+data;
});
Then from another casync function:
let readmeWithTitle=yield addTitleToReadme("#This is a Good Title",next);
console.log(readmeWithTitle);
Or from outside a casync function:
addTitleToReadme("#This is a Good Title",(err,readmeWithTitle)=>{
console.log(err || readmeWithTitle);
});
You could syntax sugar away the next
and maybe the done
callbacks to get something like:
sync function addTitleToReadme(prepend){
let data = wait fs.readFile('README.md');
return prepend+"\n"+data;
});
This would provide straightforward asynchronous code without extra state machines or caching layers (without promises), with better encapsulation (no promises to be passed around) and with better performance. It would allow you to program in normal direct style all the time just like with synchronous code.
With the generator based implementation, it 's fairly easy to do proper error handling as described in the github readme.
Maybe this has been considered before?