Async/Await are keywords from JS. It is generally used in asynchronous programming.
You probably will need to use Async/Await in two scenarios.
At first, we will understand what is asynchronous and synchronous.
Synchronous tasks are those which are bound to happen one after another. So, here 2nd(later) task gets dependent on 1st task’s completion.
Asynchronous tasks are totally opposite of synchronous tasks. Unlike synchronous tasks, 2nd(later) task does not depend on 1st task’s completion.
E.g.: I’ll take example of food order
Synchronous: You are working from home. you go to restaurants, order food, and wait for food to be prepared once prepared and served you eat and then you return home.
Asynchronous: You are working from home and at some point, of time You order food. You continue working food gets delivered and then you eat.
In the first example you had to stop your work completely until you finished eating and get back home.
In this example eating food task is completed dependent on prior task which are sequenced as going to restaurant, ordering, and eventually waiting.
In the second example you order food and resumed your work till the food gets delivered.
In this example your eating is not dependent on the previous task which is doing work. And the sequence goes like Work, Order food, Work, Food arrives and finally you eat.
You can read about promise chaining using this link
Using promises - JavaScript | MDN (mozilla.org)
Understanding the keywords and their responsibility
Async keyword is used to declare a function which returns a new Promise every time it is called, which will be resolved with the value returned by the async function or rejected with an exception uncaught within the async function [1].
Await keyword is used to call async function. It stops the current function or code execution until the returned promise is fulfilled or rejected.
Understanding promises
We know that async function returns promises which then eventually fulfills as resolve or reject.
We need to understand what promise actually is.
I will take the example of food ordering again
I hope this clears the Promise concept.
Understanding behaviors of Async/Await through scenarios
Scenario 1: Calling Async function in Asynchronous way
In this scenario we call the Async function without using await keyword. Function returns the promise with “pending” state immediately and rest of code below the function call continues running. When promise eventually gets fulfilled or rejected it updates the promise state as “fulfilled” or “rejected” respectively. In this way one or multiple independent call can be done without disturbing main flow.
Scenario 2: Calling Async function in synchronous way
In this scenario we call the Async method with await keyword. Function returns the promise with pending state immediately and await keyword makes the code wait for the promise status to be settled (rejected or fulfilled). After the promise gets settled rest of the code continues running. In this way if a code written below the await call depends on the return value of the function will get the value.
Scenario 3: Calling multiple Async functions parallelly but while doing parallel calls code should wait for all promise settlements.
This might be confusing to many of you, so I’ll explain it a bit further with an example. Let’s say you query multiple databases hosted on different machine because you want to do the reconciliation. In this scenario you will have to query two databases parallelly to save time. Here your reconciliation code should not work until both query runs successfully and gets the data. I hope you have understood the scenario. Now I’ll go to the solution part. To handle this scenario promise object has three methods
These method takes iterable(array) of promises as input and returns a single Promise. You can read about these methods and how to use them on Promise - JavaScript | MDN (mozilla.org).
Conclusion
In conclusion, understanding the behaviour of async/await in JavaScript is crucial for effective asynchronous programming. Async/await provides a more readable and simplified approach compared to promise chaining. By using the async keyword, we can declare functions that return promises, which can be resolved or rejected based on their execution. The await keyword allows us to pause the execution of code until a promise is fulfilled or rejected, ensuring that dependent tasks are handled correctly.
Promises play a significant role in asynchronous programming, acting as a placeholder for future values. They allow us to handle tasks independently and receive the results once they are ready. Whether the promise is fulfilled or rejected, we can handle the outcome accordingly.
Through different scenarios, we explored the practical implementation of async/await. Calling an async function asynchronously allows us to continue executing code without waiting for the promise to be fulfilled. On the other hand, calling an async function synchronously with the await keyword ensures that the code waits for the promise to settle before proceeding.
In situations where parallel calls to multiple async functions are required, we can utilize methods like Promise.all() or Promise.allSettled() depending on your requirement of all promises resolve or all promises settle respectively before moving forward. These methods provide effective ways to handle scenarios where multiple independent tasks need to be completed before proceeding.
Reference
[1] async function - JavaScript | MDN (mozilla.org)
Over the 3 years of my professional journey from a developer to tech lead has not only enriched my career but also played a significant role in my personal growth. The opportunities and challenges brought lots of learnings for me that I can’t be thankful enough.
One of the most significant elements this journey helped me is providing more strength to my communication skills. This allowed me to express myself explicitly to the other person. I truly learned the meaning of “communication skill is not about talking fluently in some specific Language”.
I look forward to share these basic concepts of communication skill to the new joiners and family members as well.
Second, I have learned the true meaning of Agility and adaptability. Twist and turns often comes through project lifecycle. I have learned to adapt from these types of situations. The way I have exceled through this type of situation is constructing a set of predefined action and best probable reaction by experience or some advice from seniors.
Furthermore, I have understood the usage of agile methodology. I try to use some concepts of agile methodology in my day-to-day life. Like breaking bigger tasks to smaller ones, Doing a retrospection. These things I used to do previously but now I am more mindful about them. Also, it just clicks easily when to do what.
Finally, this journey has sparked a sense of continuous learning and self-improvement. I recognized the importance of staying updated with industry trends and honing my skills. This dedication to growth has influenced my personal pursuits, as I now seek opportunities to expand my knowledge and pursue new passions.
In conclusion, the skills I acquired, the challenges I faced, and the values I embraced have had a far-reaching impact on my personal life, making me a more effective communicator, responsible individual, resilient thinker, empathetic friend, and continuous learner. As I continue on this journey, I am grateful for the opportunities that have transformed not only my career but also my perspective on life
What is the reference data type?
Reference data type is used to denote a row of a particular list.
For example, if I say I have a reference field of “employee list” and that field has some data assigned to it. This means that the reference field is denoting some row of “employee list”.
When we say data type, It often comes to our mind as a format of data with certain rules and standards. In general programming, we define the datatype of variables and then save the value assigned to it. Likewise for reference data type variables (fields), if there is no data assigned to the field that means it is not denoting any row of the particular list and vice versa.
In short
Reference data type field with data assigned to it -> Denotes a row of the particular list.
Reference data type field with empty value ->Does not denote any row.
Structure of reference data type fields
When we define a reference data type field it requires a list associated with itself and a name.
For example, if I want a reference field of “employee list” I’ll select that list whenever I’ll be defining the reference field. Like all the fields you also need to provide a name for the reference field.
If I have to define one reference field of “employee list”. I’ll write the name of the field as “employee” and select ”employee list” as the associated list.
How to get row data from reference fields
We can get any data of the denoted row of the “employee list” using the “->” operator.
For example “employee list” has multiple columns like employee name, employee no, employee activity status etc. If I want to access the employee name then I can just write an expression like employee(reference field) -> employee name and it will fetch the employee name. If the field is empty then you won’t get any data. (if you are wondering how to assign data to a reference field then I should let you know that part will come later.)
Before learning how to assign data to a reference field we should understand the concept of Row positioning.
Row positioning is clearly defined as the selected row of a list. It is also referred to as the current row of the list in WEM.
Row positioning is dependent on many events which are listed below
Note: If you navigate to a different page using the navigation menu the positioning will be reset which means there will be no row for the current position.
Assignment of reference field
Step 1: Set the row position using above listed events in the “row positioning section”.
Step 2: Select field as assignment node in the flow and then for that node choose “set to current row” option
Eg: let’s say I have a reference field of the employee list named “Emp”. I need to find an employee using employee ID and get the reference and assign it to the reference field “Emp”.
We will take a list action node. Select the “goto first row” action. Apply condition for “goto first row that matches”. The condition will be that the employee ID you want to match should be available in the column of employee ID in the employee table. So we can write something like employee ID field from input = employee ID column of employee table.
Flowchart