2 ms·
As a fan of go, I was also a bit disappointed at all the gotchas and how easy it is to make it wrong: if let result = await db.post({}); works, why can't I
by rakoo 12y ago
As a fan of go, I was also a bit disappointed at all the gotchas and how easy it is to make it wrong: if
let result = await db.post({});
works, why can't I
docs.forEach((doc) => await db.post(doc))
? I still have to keep promises somewhere in my head and work around them.
But I saw this post [0] the other day where the author reuses csp and go stuff and introduces them to js to basically achieve asynchronous actions in a synchronous manner:
function dbPost(doc) {
var ch = chan();
db.post(doc).then(function (res) {
csp.putAsync(ch, res);
})
return ch;
}
docs.forEach((doc) => yield csp.takeAsync(dbPost(doc)))
or, for your code:
go(function* () {
let db = new PouchDB('mydb');
let result = yield take(dbPost({}));
let doc = yield take(dbGet({}));
console.log(doc);
})
There still is some boilerplate compared to a fully synchronous code (and it introduces the notion of channels, which do have an added value other than just asynchronously waiting for a result), but overall I see it much more capable at the task than what is described in OP's article.
[0] http://jlongster.com/Taming-the-Asynchronous-Beast-with-CSP-in-JavaScript http://jlongster.com/Taming-the-Asynchronous-Beast-with-CSP-...
- thesorrow 12y agoYes js-csp is awesome and there's various advanced operations implemented like pipelines