3 ms·
link to example please
by grandalf 11y ago
link to example please
- 15155 11y agoasync function foo() { try { const valueOne = await promiseReturningFunction(); const valueTwo = await anotherAsyncFunction(valueOne); return valueTwo; } catch (err) { alert(err); } } foo(); //Returns a Promise. ---- In this example, I've sequenced the resolution of two promises (async functions return Promises when invoked) If either of these Promises reject or throw an error, or if any of the code within the try {} block throws an error, the error will be caught inline in the catch block (one area only). You can also see that both Promise's return values are in-scope and continually usable (vs. losing scope with 'then' chains).
- grandalf 11y agoI see. Not sure what the best practice is for the losing scope in then chains issue with promises. I ended up creating something along the lines of a message object designed for each chain, which felt like a smell.
- 15155 11y agoFeels like an un-compiler-assisted State monad. IMO threading an object through is probably the best method, unfortunately. You could use an Immutable Record or something to help keep it under control. I would personally just use async/await to more explicitly handle the sequencing/binding.
- grandalf 11y agoAgreed. I think I'll refactor... thanks for the example.