Can someone explain to me the attraction of async programming? I don't really do JS, where a lot of this seems to be happening, but the code I have seen with the huge ladders of callbacks doesn't seem so great to work with to me. Also, although using promises seems better, it seems like it could quickly become spaghetti.
If you specifically mean async/await syntax, let me illustrate with a contrived example. It can let you express a sequence of asynchronous operations in a more natural way:
function promised(cache, db, metrics) {
return cache.query(...).then(cachedResult => {
if (cachedResult) {
return cachedResult;
} else {
return db.query(...).then(dbResult => {
return cache.store(dbResult).then(_ => dbResult);
});
}
}).then(finalResult => {
metrics.log(...);
return finalResult;
});
}
async function awaited(cache, db, metrics) {
let result = await cache.query(...);
if (!result) {
result = await db.query(...);
await cache.store(result);
}
metrics.log(...);
return result;
}