4 ms·
I don't get what's the point of his article. Promises and async/await are here for ages. Moreover it does not highlight issues with await such as if you forget
by hdhzy 9y ago
I don't get what's the point of his article. Promises and async/await are here for ages. Moreover it does not highlight issues with await such as if you forget to await a promise you've just introduced a subtle but in your code. Sounds unlikely? Check out the last snippet in the article (waitForHello(2000) missing await, doesn't do any harm in this case as there are no code after it but it's a hidden bug).
- CodeWriter23 9y agoI also am confused by using the Pyramid of Doom as a pseudo rationale for needing this. Pyramid too tall? Use a function call. Nothing at all wrong with: function multiStepAsyncProcess() { hitServer(params, function() { multiStepAsyncProcessStep2(); }); } function multiStepAsyncProcessStep2() { hitServer(params, function() { ... }); }
- rmrfrmrf 9y agoNow do it with error handling and recovery.
- xg15 9y agoMoreover, async/await allows you to preserve multiple intermediary results between async steps - which is awkward in both promises and un-nested pyramids like the OP's: (not even starting with conditions, loops and recursive calls that contain async steps...) Async/Await: async function f(a) { var b = await foo(a); var c = await bar(a, b); return baz(a, b, c); } Promises: function f(a) { var _b; return foo(a) .then(b => { _b = b; return bar(a, b); }) .then(c => baz(a, _b, c)) } Callbacks: function f(a, done) { foo(a, b => bar(a, b, c => baz(a, b, c, done))); } Callbacks untangled: function f(a, done) { var _b; foo(a, step2); function step2(b) { _b = b; bar(a, b, step3); } function step3(c) { baz(a, _b, c, done); } }
- CodeWriter23 9y ago<shrugs> ok function multiStepAsyncProcess() { hitServer(params, function(err) { if(err) { handleError(err); return; } multiStepAsyncProcessStep2(); }); } function multiStepAsyncProcessStep2() { hitServer(params, function(err) { ... }); }
- always_good 9y agoThat just "fixes it" with indirection, which is the only tool you have with callback style.
- CodeWriter23 9y agoActually I characterize it as opting out of newly-introduced madness, and using the techniques we used pre-madness. Not a “fix” so much.
- always_good 9y agoI can agree with that. I think the problem with these sorts of discussions starts when someone dismisses promises, at which point one must assume that the person dismisses the problems that promises solve. And I think that happens in the other direction too, when someone dismisses callbacks which suggests that they think that callbacks are intractable in all scenarios.
- saas_co_de 9y agoYou are right. This has been a common error for me when writing code using async/await, but IMO it is not worse than forgetting to return promises in promise chains, so async/await is still a massive improvement over plain promises for me.