Is async / await useless?

Fun Fun Function · Intermediate ·🌐 Frontend Engineering ·8y ago

Key Takeaways

Examines the usefulness of async/await in systems design and development

Full Transcript

good Monday morning today we're embarking on a little thought experiment is a sink a weight really useful or is it just fluffy sugar for people that can't be bothered to learn functional programming I am mg J's and I'm David you are watching function [Music] [Music] you may notice that there are two people here instead of the ordinary one person you might recognize David from earlier episodes yeah I wasn't the facebook messenger both pair programming video and I'm also doing a lot of business stuff behind the scenes behind the camera today's episode is sponsored by tip top because they are currently hiring if you know someone who would like to work with react native yay no js' mmm in the heart of Stockholm then please send them to a tip at comm / FF that link is also in the episode description it's a second wait really that useless wait well I don't know really the thing is everybody went a sink away to arrived on the scene everyone was really really excited about it and finally we can program like we used to do in the old days synchronously yeah exactly in that that's it's kind of like the the thing that irks me about it it's a way of kind of hiding that things are a synchronous but not really and I don't know it the idea behind it and I made an entire episode in the only a sink await keyword is to create write code that is a synchronous but flows like synchronous code the thing is we already have sort of that with promises and I would just like to attack I think await for a little bit like really attempt to where for every like nine so sync away to example out there try to really write some nice promise code and see if what like how big of an improvement is this really when it comes down I I posted like yesterday when recording this video I posted a thread on the front on forum asking this same question and see that hey can we just help each other out and try to post some examples where we really try to like make a sink away shine as much as possible and then just try to rewrite that with promises as well we can and people post some really really good stuff all right so why don't you that isn't super familiar with this example walk through it all right we have this a synchronous function get hamburger it takes a server and a default burger so we'll check here if and then we are wait the server has any hamburgers if we don't have that let's just before return the default burger and then we check do we have eaten any burgers and we just make an ordinary call get number of eaten burgers today yeah so that call is synchronous that's a synchronous okay and then we check for max burgers so we have we can see that do we have a maximum number of burgers that we allow to eat every day and but we have to ask the server for that yeah and that is a synchronous so we await for that and then before eaten less burgers than we are allowed to then we can ask the service to load us a burger yeah and if we like if we're having then we just return the default burger yeah so this is of course a silly example like and it also has horrible software architecture like why is the get number of burgers eaten today synchronous but the maximum number of burgers per day it's like it seems like it should be eating worse but like a Magnus what is this but that's not the point of this thing like the point is to find an example where a sinker weight really works so let's let's try to look at I try to rewrite this all right it wasn't just me like I it was a combination of the other four members and and me try to rewrite this into a synchronous flow and this is what we ended up with we have like just like the previous function we except the server and a default default Burger then we call server has burgers which returns a promise which resolves into this has burgers as burgers boolean and if it doesn't have any burgers on the server we return the default burger and then we otherwise we revert to calling get max number of burgers per day and once we receive that we can get the number of burgers today and compare that to the backs burgers that we received and if we are allowed to eat more burgers today we load burger otherwise we just return the default burger as one does in enterprise-grade programming so I sort of like this but it starts to feel like the whole purpose with promises was to like get out of this callback hell yeah because everything got nested and now we're back into the nested world yeah this example is chosen because it gives this the sample burger example like it has no nesting because this is a these variables here are available to the entire function we basically write a sort of a shared global State for the function and that means that we it kind of it becomes like the nesting is it's not there but if we have a look in the in the refractor we see that we actually get a level of nesting it's not too bad it's just one level of nesting really it looks more here because I indented so that you can watch it on a tiny screen but it's it's there's still some indentation that said I think that I'm doing quite a bit of clever stuff here as well which makes it more yeah it's the the ternary expressions is something that you I tend to fall into using because yes feels cool to just have these one-liner functions which use ternary expression then I used to turn expressions a year like like it just feels cool to do this but if I actually just allow myself to fall out of use turn Aries and allow the co2 sprawl a little bit this is the same code except that I use like these multi line functions instead and just use if statements instead of the turn Aries and now the code doesn't look all that scary it looks pretty similar to the image I think the weight code yeah exactly lets me show you that again like suddenly it doesn't look all that scary in fact I think that almost the the promise example almost looks bit even more straightforward because we like the asynchronous flow is more in your face depending on how if you want that in your face or not really but if you like IKEA takeaway for me when I did this was that huh a lot of the asynch awaits examples out there look often look more approachable and nice because there isn't a lot of cleverness going on in them because promises tend to for at least for me encouraged a bit of cleverness and then return Aries and stuff like that which just isn't mmm for me like like that might look scary but if you do it like this it's kind of approachable so here we have another example we have a synchronous function process all users in earth we call the database and we wait for it get the users from the database but then we want to iterate through each user and process them in some way and we need each user's ID to do that and then we wait for the result before we continue on yeah exactly so this processes each users quench aliy this is actually the the example that I used in the sink of wait video to show where a sink of weight really works well compared to promises and I I do a refactoring in that video where I use like a reduce to to create like this this promise promise thingamabob but that replaces it which which isn't too bad but I kind of like to revisit it and see if we can do it even better than that this is also real code from an application sort of with that I go right across where we actually needed to do something like this let's have a look at the refactoring I ended up with a lot of people help up with this as well so if you if you're a member of the fund conform you can check out the full thread by clicking here or that's it's also linked in the episode description so there's a lot discussion going on around this topic and a lot of the farm topic yes it turned out to be really really interesting alright so this is what I ended up with my initial solution where this was much worse but Chris Collins really nailed it with these cool destructuring things so let me walk you through this we do the DB query just like the earlier thing but then we get a promise and the users we pass them into this wait for each function and we also pass in this processing function so for each we wait for each user to be processed here so we call process user where the user ID and that will return a promise which we will wait for and then just promises like just like the other one all right so wait for each is a generic function it's not really aware of abusers it's not at all aware abusers it takes as its first argument this process function which is this one and as its second argument it takes an array and it uses a raid structure in here to break apart the head from the tail the right so head will be like the first item of the array and then tail is going to be like the remainings its first remaining the remainder is an array and the item is not an array precisely and let's disregard this for now because that's that's on the second loop when we get the head here we pass it to process function and process function will process the user return a promise which we then wait for it to resolve and then we create a new wait for each function here we recursively call ourselves precisely so we call ourselves we're like the rest of the tails so to speak so we processed the head and now we process the tail which is then all the users and then we like sort of just dick dick dick dick cut off the head and yeah keep on working downwards exactly so we used this is a very standard this is standard functional programming you saw instead of using loops we you use recursive functions which like get a smaller and smaller and smaller part of our Reese part of our result set all the time so we cut off the head pass down the tail cut off the ted pass down the tail but then we have to have an end case as well exactly and that's what happens here it's when we when we don't have a head anymore we just poof resolved and then we're done I like this because it's pretty neat because it's recursion and I just like recursion because it just feels it's very I know it's oddly what you mean it just feels good somehow it's it's a very satisfying way of programming it's it's a bit tricky to reason about but it's also at its essence very simple so like this case here it was hard for me to figure it out but once I have it like the code is so simple it just look and I know it's how all of this this weight for each is so it's it is generate because you can use it for for anything basically yeah and I feel like this is kind of like the drawback and the advantage of using promises at the same time mentioned on the forum that I felt like promises tend to encourage cleverness and scale I said yeah but you could also think about it as if they encourage you to think about your code oh so what what happens here I actually found that we could break this apart into a generic case so now we can use if we have more of this kind of sequential processing we now have a generic function to do that and probably to be honest in if there is a functional promise library this function would probably be in it so that we wouldn't have to write it so promises tend to make you think about composability and what can we extract while a sinker weight tends to not quite do that because the the the it doesn't become quite obvious that it might have some room for decoupling or extraction here and also I think it's very I it's a very clear Co always like when it's a clear coat like you understand what's going on you return there the query you get the users and then you have to process each user wait for them and your Protestant it's just simple and clear all right so there's more stuff in the forum topic that I want to talk about there's there's a really really good example of where I think a way it really does well and it's super tricky to get work which is basically when you have multiple things like when you start like aggregating data start collecting data into one thing this is also error handling that's my main topic I love how are handling like promises when you need to like diverge and handle up handle stuff as you go like promise has become a lot more appealing but this episode is coming up on 50 minutes or so and I think that I want to say that for next week and like cut this off and like just summarize what we talked about sure so we looked at the burger example we collect burgers and see if we have burgers on the server and the refactoring looked a little bit like this it gave us more indentation so it doesn't have the flat appeal of the ascetic of weight but it's really it's still not overly horrible to to deal with I could still very straightforward and easy to reason about and especially when we broke it up and didn't we didn't do much cleverness at all then we looked at this example which did sequential processing our user and we were just sort of like the best case for I think your weight right yeah this is the this is the typical example case of using I think awaits yeah I think that this is a very a lot of people use this to sell think away don't say I like it no it's it's fine I like I don't want to too much on a sink away that's not the point of this episode this is just challenging like an ocean yeah and see you like if is this really correct like a weed there's no hate against a sink away and this is what we ended up with it which is this a little library function called that we call wait for H this is probably in a lot of functional programming languages that just recursively processes an array and assumes that every process every call to this returns promise and then it just keeps chaining it and it turned out okay and again today's episode is sponsored by tip top and they are currently hiring if you know someone who would like to work with react native and nodejs in the heart of Stockholm please send them to tip-top calm slash FFF that link is also in the episode description you have just watched an episode of fun fun function I released these every Monday morning oh wait under GMT if you are forgetful you can subscribe by clicking here or you can watch another episode right now by clicking here I am mpj and I'm David until next Monday morning stay curious

Original Description

💖 This episode is sponsored by Tiptapp, they are currently hiring. If you know someone that would like to work with React Native or node.js in the heart of Stockholm, send them to http://tiptapp.com/fff 🔗 Support the show by becoming a Patreon https://www.youtube.com/watch?v=trP9q703x0o 🔗 Fun Fun Forum topic for the video https://www.funfunforum.com/t/thought-experiment-is-async-await-just-useless-sugar-for-people-that-dont-want-to-do-functional-programming/4299/65 🔗 Async / Await video https://www.youtube.com/watch?v=568g8hxJJp4 🔗 Pair programming a messenger bot video https://www.youtube.com/watch?v=zFO1cRr5-qY 🔗 mpj on Twitter https://twitter.com/mpjme 🔗 Help translate the show to your language http://www.youtube.com/timedtext_cs_panel?tab=2&c=UCO1cgjhGzsSYb1rsB4bFe4Q I don’t actually believe that, but I would like to do a thought experiment where we test the hypothesis that all async / await code in JavaScript can be re-written just as well using promises, and that async / await doesn’t really add much value to JavaScript. 💛 Follow on Twitch and support by becoming a Subscriber We record the show live Mondays 7 AM PT https://twitch.tv/funfunfunction 💛 Fun Fun Forum Private discussion forum with other viewers in between shows. https://www.funfunforum.com. Available to patron members, become one at https://www.patreon.com/funfunfunction 💛 mpj on Twitter https://twitter.com/mpjme 💛 CircleCI (Show sponsor) Robust and sleek Docker-based Continuous Integration as a service. I used CircleCI prior to them becoming a sponsor and I love that their free tier is powerful enough for small personal projects, even if they are private. Use this link when you sign up to let them know you came from here: https://circleci.funfunfunction.com 💛 Quokka (Show sponsor) Wonder how MPJ evaluates JavaScript inline his editor. Quokka is the answer - use this link when you buy to let them know you came from here: http://quokka.funfunfunction.com 💛 FUN FUN FUNCTIO
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Fun Fun Function · Fun Fun Function · 0 of 60

← Previous Next →
1 Higher-order functions - Part 1 of Functional Programming in JavaScript
Higher-order functions - Part 1 of Functional Programming in JavaScript
Fun Fun Function
2 Map - Part 2 of Functional Programming in JavaScript
Map - Part 2 of Functional Programming in JavaScript
Fun Fun Function
3 Reduce basics - Part 3 of Functional Programming in JavaScript
Reduce basics - Part 3 of Functional Programming in JavaScript
Fun Fun Function
4 Destructuring: What, Why and How - Part 1 of ES6 JavaScript Features
Destructuring: What, Why and How - Part 1 of ES6 JavaScript Features
Fun Fun Function
5 Reduce Advanced - Part 4 of Functional Programming in JavaScript
Reduce Advanced - Part 4 of Functional Programming in JavaScript
Fun Fun Function
6 Closures - Part 5 of Functional Programming in JavaScript
Closures - Part 5 of Functional Programming in JavaScript
Fun Fun Function
7 Too many tools and frameworks!
Too many tools and frameworks!
Fun Fun Function
8 Currying - Part 6 of Functional Programming in JavaScript
Currying - Part 6 of Functional Programming in JavaScript
Fun Fun Function
9 Recursion - Part 7 of Functional Programming in JavaScript
Recursion - Part 7 of Functional Programming in JavaScript
Fun Fun Function
10 Promises - Part 8 of Functional Programming in JavaScript
Promises - Part 8 of Functional Programming in JavaScript
Fun Fun Function
11 Staying relevant as a programmer
Staying relevant as a programmer
Fun Fun Function
12 Factory Functions in JavaScript
Factory Functions in JavaScript
Fun Fun Function
13 Composition over Inheritance
Composition over Inheritance
Fun Fun Function
14 Software needs to be better - FunFunFunction #1
Software needs to be better - FunFunFunction #1
Fun Fun Function
15 Unit testing: How to get your team started - FunFunFunction #2
Unit testing: How to get your team started - FunFunFunction #2
Fun Fun Function
16 Straight-line code over functions - FunFunFunction #3
Straight-line code over functions - FunFunFunction #3
Fun Fun Function
17 Clojure - FunFunFunction #5
Clojure - FunFunFunction #5
Fun Fun Function
18 The growth stages of a programmer - FunFunFunction #6
The growth stages of a programmer - FunFunFunction #6
Fun Fun Function
19 5 tips to quickly understand a new code base - FunFunFunction #7
5 tips to quickly understand a new code base - FunFunFunction #7
Fun Fun Function
20 Semicolons cannot save you! - FunFunFunction #9
Semicolons cannot save you! - FunFunFunction #9
Fun Fun Function
21 Functors - FunFunFunction #10
Functors - FunFunFunction #10
Fun Fun Function
22 Functors: I was WRONG! - FunFunFunction #11
Functors: I was WRONG! - FunFunFunction #11
Fun Fun Function
23 Questions and Answers - FunFunFunction #12
Questions and Answers - FunFunFunction #12
Fun Fun Function
24 Streams - FunFunFunction #13
Streams - FunFunFunction #13
Fun Fun Function
25 Prototypes in JavaScript - FunFunFunction #16
Prototypes in JavaScript - FunFunFunction #16
Fun Fun Function
26 Fast or Flexible? - FunFunFunction #17
Fast or Flexible? - FunFunFunction #17
Fun Fun Function
27 Coders are herd animals - FunFunFunction #18
Coders are herd animals - FunFunFunction #18
Fun Fun Function
28 Weekend Kubernetes Shenanigans - FunFunFunction #19
Weekend Kubernetes Shenanigans - FunFunFunction #19
Fun Fun Function
29 Monad - FunFunFunction #21
Monad - FunFunFunction #21
Fun Fun Function
30 Moar Weekend Shenanigans - FunFunFunction #23
Moar Weekend Shenanigans - FunFunFunction #23
Fun Fun Function
31 Questions and Answers - FunFunFunction #24
Questions and Answers - FunFunFunction #24
Fun Fun Function
32 Losing motivation - FunFunFunction #25
Losing motivation - FunFunFunction #25
Fun Fun Function
33 LONGEST KUBERNETES SHENANIGANS! - FunFunFunction #26
LONGEST KUBERNETES SHENANIGANS! - FunFunFunction #26
Fun Fun Function
34 Fast code is NOT important - FunFunFunction #27
Fast code is NOT important - FunFunFunction #27
Fun Fun Function
35 Pair Programming a Facebook Messenger Bot - FunFunFunction #28
Pair Programming a Facebook Messenger Bot - FunFunFunction #28
Fun Fun Function
36 Writing unit tests for personal projects? - FunFunFunction #29
Writing unit tests for personal projects? - FunFunFunction #29
Fun Fun Function
37 Let's Code a Pomodoro Button - FunFunFunction #30
Let's Code a Pomodoro Button - FunFunFunction #30
Fun Fun Function
38 What editor do you use? - FunFunFunction #31
What editor do you use? - FunFunFunction #31
Fun Fun Function
39 Arrow functions in JavaScript - What, Why and How - FunFunFunction #32
Arrow functions in JavaScript - What, Why and How - FunFunFunction #32
Fun Fun Function
40 Is Programming Art? - MPJ's Musings - FunFunFunction #33
Is Programming Art? - MPJ's Musings - FunFunFunction #33
Fun Fun Function
41 Generators in JavaScript - What, Why and How - FunFunFunction #34
Generators in JavaScript - What, Why and How - FunFunFunction #34
Fun Fun Function
42 Haskell Basics - FunFunFunction #35
Haskell Basics - FunFunFunction #35
Fun Fun Function
43 Haskell - Baby's first functions - FunFunFunction #36
Haskell - Baby's first functions - FunFunFunction #36
Fun Fun Function
44 Is Big O relevant to you? - Q&A Part 1 - FunFunFunction #37
Is Big O relevant to you? - Q&A Part 1 - FunFunFunction #37
Fun Fun Function
45 How much are you allowed to Google? - Q&A Part 2 - FunFunFunction #38
How much are you allowed to Google? - Q&A Part 2 - FunFunFunction #38
Fun Fun Function
46 Haskell lists - FunFunFunction #39
Haskell lists - FunFunFunction #39
Fun Fun Function
47 var, let and const - What, why and how - ES6 JavaScript Features
var, let and const - What, why and how - ES6 JavaScript Features
Fun Fun Function
48 Why are some programming languages popular? - MPJ's Musings  - FunFunFunction #41
Why are some programming languages popular? - MPJ's Musings - FunFunFunction #41
Fun Fun Function
49 Does a developer need to be nice? - MPJ's Musings - FunFunFunction #42
Does a developer need to be nice? - MPJ's Musings - FunFunFunction #42
Fun Fun Function
50 bind and this - Object Creation in JavaScript P1 - FunFunFunction #43
bind and this - Object Creation in JavaScript P1 - FunFunFunction #43
Fun Fun Function
51 Examples of this and bind - Object Creation in JavaScript P2 -  FunFunFunction #44
Examples of this and bind - Object Creation in JavaScript P2 - FunFunFunction #44
Fun Fun Function
52 Prototype basics - Object Creation in JavaScript P3 - FunFunFunction #46
Prototype basics - Object Creation in JavaScript P3 - FunFunFunction #46
Fun Fun Function
53 Separation of concerns RANT - MPJ's Musings - FunFunFunction #47
Separation of concerns RANT - MPJ's Musings - FunFunFunction #47
Fun Fun Function
54 Cellular Automata - Pair Programming - FunFunFunction #49
Cellular Automata - Pair Programming - FunFunFunction #49
Fun Fun Function
55 The 'new' keyword - Object Creation in JavaScript P4 - FunFunFunction #50
The 'new' keyword - Object Creation in JavaScript P4 - FunFunFunction #50
Fun Fun Function
56 __proto__ vs prototype - Object Creation in JavaScript P5 - FunFunFunction #52
__proto__ vs prototype - Object Creation in JavaScript P5 - FunFunFunction #52
Fun Fun Function
57 Unity game pair programming - Let's code - FunFunFunction #53
Unity game pair programming - Let's code - FunFunFunction #53
Fun Fun Function
58 Throw out your tools - MPJ's Musings - FunFunFunction #54
Throw out your tools - MPJ's Musings - FunFunFunction #54
Fun Fun Function
59 Unit tests vs. Integration tests - MPJ's Musings - FunFunFunction #55
Unit tests vs. Integration tests - MPJ's Musings - FunFunFunction #55
Fun Fun Function
60 Object.create - Object Creation in JavaScript P6 - FunFunFunction #57
Object.create - Object Creation in JavaScript P6 - FunFunFunction #57
Fun Fun Function

Related Reads

Up next
Elementor Angie Ai Plugin Tutorial
Quick Tips - Web Desiign & Ai Tools
Watch →