Flutter’s await vs. callback by ChatGPT
ChatGPT has been a hot potato on the internet for the last couple of weeks. I’ve been using ChatGPT almost since day one and getting a lot of help from it, spending less time on the internet or StackOverflow for software development questions. SO’s answer is generally way better and more accurate for the recent technologies, but figuring out the solution after reading all those comments and reply is somewhat painful.
I’ve been learning and using Flutter for my experimental mobile app development, and ChatGPT has been a great help in speeding up this new tech (to me). Since I don’t often post about software development topics here, even though that’s the job that gives me a paycheck, I would like to post some of my “stupid” questions to ChatGPT and share what ChatGPT says.
Q: In Flutter, what’s better in terms of performance perspective between using await and “then” callback in Future function?
The question was after reading a few of the Flutter/Dart’s Future-return-related and how to handle these. I was expecting using then
and callback would be more effective than adding await
everywhere, but also it just reminds me of callback hell that I experienced in other programming languages/frameworks. So I considered using await
to make things simple to maintain and easy to read unless there is a considerable performance loss.
Then, below is the answer that ChatGPT gave to me.
ChatGPT: In terms of performance perspective, using
then
callback is generally better than usingawait
when working withFuture
in Flutter.The reason for this is that when you use
await
, the calling function blocks and waits for theFuture
to complete before continuing to execute the rest of the code. This can lead to reduced performance as it may cause your application to appear unresponsive or sluggish. On the other hand, when you usethen
callback, the calling function can continue to execute while theFuture
is being processed in the background, which can result in better performance.However, there may be cases where using
await
is necessary or more readable than usingthen
. For example, if you need to perform multiple asynchronous operations in a specific order, usingawait
can make your code more concise and easier to read. Additionally, usingawait
can make error handling simpler as any exceptions thrown by theFuture
can be caught in a single try-catch block.In summary, using
then
callback can be better for performance in most cases, but the choice betweenawait
andthen
ultimately depends on the specific use case and the needs of your application.
Although it does not surprise me that much in terms of the answer I was expecting, I am amazed by how ChatGPT can create a tech-related statement seamlessly. I don’t have a code block that ChatGPT created here, but other answers that ChatGPT made for my other Flutter-Firebase-related question were pretty helpful. Of course, it’s not a real-time updating AI model, so sometimes the solution doesn’t quite work as-it-is, but whoever has some coding experience can quickly figure out what to do to fix the code.
How do you like ChatGPT so far? Especially as a software engineer if that’s your current role at work?