3 ms·
> To see that for yourself, try to re-write our async code snippet again using only async/await statements and see how does that pan out. With async/await, wou
by jmull 8y ago
> To see that for yourself, try to re-write our async code snippet again using only async/await statements and see how does that pan out.
With async/await, wouldn't the example just be:
let aOut = doA();
let bOut = doB();
let cOut = doC(aOut);
doD(bOut);
doE(cOut, aOut, bOut);
That is, it looks the same as the sync version.
The difference is in the "do" functions themselves: they would be async (meaning they return a promise, and they accept promises as parameters).
- lorean_victor 8y agoso in your code sample, would 'aOut' be a promise or a value? in case it is a promise, then basically doC() would needed to be re-written to handle when it gets a promised value instead of an actual value, and if not, then doA() must be invoked synchronously, i.e. the `let bOut = doB()` statement must be awaiting `let aOut = doA()` statement which is not what the desired async execution flow is.
- jmull 8y agoYes, aOut would be a promise. It would look like this: async function doA() { ... return "the-result-of-a"; } (Which returns a promise. Assuming execution reaches that last line of my sample code, the promise will resolve to "the-result-of-a" when you "await" it.) > ...then basically doC() would needed to be re-written to handle when it gets a promised value instead of an actual value Well, wait a minute... it wasn't written in the first place! Let's write it though. It would look like this: async function doC( param ) { ... // we use "await param" where we want to // access the value of param. e.g.: const paramValue = await param; ... } So not bad.
- lorean_victor 8y agoyeah really not bad at all, didn't think of that solution originally. edited the article accordingly.