Debbie Clay

Debbie Clay

1565939035

async/await vs then

As I’ve stated in a lot of other posts, I’m a big fan of async/await. I think it’s a pretty clean way to manage code your synchronous and asynchronous code.

Async/Await awesomeness

I want to compare some of the bad that can be avoided with async/await.

// Yucky then usage
async function getThePizza() {
       functionGettingFromMongo('someArguments').then((someData) => {
              
              // We want to use this data so we need to know that this promise completed
              doSomeWorkWithAPromise().then((moreData) => {
                      const somethingCool = someData + moreData;

                      insertToMongoNow(somethingCool).then((response) => {
                              console.log(‘success!’, response);
                      }).catch((e) => {
                              console.log(‘some error happened’, e);
                      });
              });

       });
}

So this is a function where we are entering callback hell. While .then() isn’t a callback function, codewise it reads the exact same. It’s a bunch of nested functions that get deeper and deeper and make code less and less readable.

// So good.
async function getThePizza() {
       const someData = await functionGettingFromMongo(‘someArguments’);
       const moreData = await doSomeWorkWithAPromise();

       const somethingCool = someData + moreData;
       try {
              const response = insertToMongoNow(somethingCool );
              console.log(‘success!’, response);
       }
       catch(e) {
              console.log(‘some error happened’, e);
       }
              
}

It’s a lot more readable. At the least it’s not getting more and more indented. I had been using async/await exclusively since I discovered it so much so that I started thinking of .then() as bad and kind of forgot that the beauty of programming is that almost everything is situational.

When .then() isn’t so bad

When I’m trying to scrape a lot of pages at once I don’t want to be blocked. I don’t use await but instead push the promises into an array and use Promise.all. That will ensure that I know when everything completes so I can close my db connection or puppeteer browser. Like this:

async function getThePizza() {
       
       const promises: any[] = [];
       for (let url of lotsAndLotsOfUrls) {
              promises.push(functionForHandlingTheUrl(url));
       }

       await Promise.all(promises);
       await closeMyDbConnectionOrPuppeteerOrSomething();
}

This works fine. Except for when I want to do something with the specific url after functionForHandlingTheUrl finishes with it. Now what? Using await looks like this:

async function getThePizza() {
       
       for (let url of lotsAndLotsOfUrls) {
              const something = await functionForHandlingTheUrl(url);
              await dbWorkOrSomething(something);
       }

       await closeMyDbConnectionOrPuppeteerOrSomething();
}

Very readable but much slower. I’m no longer taking advantage of any concurrency around I/O threads. So what’s the better alternative? This worked GREAT:

async function getThePizza() {
       
       const promises: any[] = [];
       for (let url of lotsAndLotsOfUrls) {
              promises.push(functionForHandlingTheUrl(url).then(something => {
                     // This parts only happens when functionForHandlingTheUrl completes and doesn’t block the rest
                      await dbWorkOrSomething(something);
              }));

       }

       await Promise.all(promises);
       await closeMyDbConnectionOrPuppeteerOrSomething();
}

Using .then() here makes it so I have one nested function, it’s true, but we are able to do stuff when only that specific iteration is ready. It doesn’t block the rest of the looping so I am still lightning fast. And I am happy.

Thanks for reading

If you liked this post, please do share/like it with all of your programming buddies!

Follow us on Facebook | Twitter

Further reading about JavaScript

The Complete JavaScript Course 2019: Build Real Projects!

Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)

JavaScript Bootcamp - Build Real World Applications

The Web Developer Bootcamp

JavaScript Programming Tutorial - Full JavaScript Course for Beginners

New ES2019 Features Every JavaScript Developer Should Know

Best JavaScript Frameworks, Libraries and Tools to Use in 2019

React vs Angular vs Vue.js by Example


#javascript #web-development

What is GEEK

Buddha Community

async/await vs then
Autumn  Blick

Autumn Blick

1598839687

How native is React Native? | React Native vs Native App Development

If you are undertaking a mobile app development for your start-up or enterprise, you are likely wondering whether to use React Native. As a popular development framework, React Native helps you to develop near-native mobile apps. However, you are probably also wondering how close you can get to a native app by using React Native. How native is React Native?

In the article, we discuss the similarities between native mobile development and development using React Native. We also touch upon where they differ and how to bridge the gaps. Read on.

A brief introduction to React Native

Let’s briefly set the context first. We will briefly touch upon what React Native is and how it differs from earlier hybrid frameworks.

React Native is a popular JavaScript framework that Facebook has created. You can use this open-source framework to code natively rendering Android and iOS mobile apps. You can use it to develop web apps too.

Facebook has developed React Native based on React, its JavaScript library. The first release of React Native came in March 2015. At the time of writing this article, the latest stable release of React Native is 0.62.0, and it was released in March 2020.

Although relatively new, React Native has acquired a high degree of popularity. The “Stack Overflow Developer Survey 2019” report identifies it as the 8th most loved framework. Facebook, Walmart, and Bloomberg are some of the top companies that use React Native.

The popularity of React Native comes from its advantages. Some of its advantages are as follows:

  • Performance: It delivers optimal performance.
  • Cross-platform development: You can develop both Android and iOS apps with it. The reuse of code expedites development and reduces costs.
  • UI design: React Native enables you to design simple and responsive UI for your mobile app.
  • 3rd party plugins: This framework supports 3rd party plugins.
  • Developer community: A vibrant community of developers support React Native.

Why React Native is fundamentally different from earlier hybrid frameworks

Are you wondering whether React Native is just another of those hybrid frameworks like Ionic or Cordova? It’s not! React Native is fundamentally different from these earlier hybrid frameworks.

React Native is very close to native. Consider the following aspects as described on the React Native website:

  • Access to many native platforms features: The primitives of React Native render to native platform UI. This means that your React Native app will use many native platform APIs as native apps would do.
  • Near-native user experience: React Native provides several native components, and these are platform agnostic.
  • The ease of accessing native APIs: React Native uses a declarative UI paradigm. This enables React Native to interact easily with native platform APIs since React Native wraps existing native code.

Due to these factors, React Native offers many more advantages compared to those earlier hybrid frameworks. We now review them.

#android app #frontend #ios app #mobile app development #benefits of react native #is react native good for mobile app development #native vs #pros and cons of react native #react mobile development #react native development #react native experience #react native framework #react native ios vs android #react native pros and cons #react native vs android #react native vs native #react native vs native performance #react vs native #why react native #why use react native

Rusty  Bernier

Rusty Bernier

1592372285

Two Ways to Do Async/Await in ASP.NET Wrong (and How to Fix Them)

My team is in the process of refactoring a large application that we want to deploy to our production environment soon. I’ve just been assigned as lead developer to this project, since the prior lead got another job in a different state, and I’ve been digging through the code to

#async/await #async #asp.net #programming

PWA vs Native App: Which Is Better Option In 2021?

Every year, the world is expanding with the launch of new smartphones and other gadgets available in the market. According to Statista, more than 50% of the population will be using smartphones by the end of 2021.

Hence, businesses worldwide have understood the importance of smartphones and are joining the mobile industry by launching native apps.

Apart from native apps, progressive web apps is another technology that is gaining a lot of attention among businesses. Moreover, various leading companies worldwide have openly accepted PWA and built progressive web apps.

Now, the question arises, how is PWA different from the native apps? Read More

#pwa vs native #pwa vs native app #progressive web app vs native #progressive web app vs native app #pwa vs native app performance

Antwan  Larson

Antwan Larson

1625041020

.NET 5 REST API Tutorial: 06 Tasks, Async and Await

Get the source code: https://youtube.dotnetmicroservices.com/net5restapi-06
My Complete .NET Microservices Learning Path: https://dotnetmicroservices.com

This is part 6 of my .NET 5 REST API Tutorial. You will learn:
• What is the asynchronous programming model
• How to use tasks, async and await to add asynchronous programming to your REST API

Blog: http://juliocasal.com
LinkedIn: https://www.linkedin.com/in/juliocasal
Twitter: @julioc

#dotnet #restapi #tutorial

#dotnet #.net 5 #async #tasks, async and await