Synchronous HTTP calls in Angular using Async and Await

Observables in Angular offer significant benefits over other techniques for event handling, asynchronous programming, and handling multiple values. But some times we may want to wait for the response from previous HTTP call or load default settings for an application. In that case, we use Async and Await functions to achieve this. This blog post explains the steps and provides code samples. As usual complete code uploaded to Github

Async & Await

An asynchronous function is a function that operates asynchronously via the event loop, using an implicit Promise to return its result. But the syntax and structure of your code using async functions are much more like using standard synchronous functions. The await operator is used to wait for a Promise. It can only be used inside an async function.

async method() 
{
  var x = await resolveAfter2Seconds(10);
  console.log(x); // 10
}

Technologies

  1. Angular 9+
  2. json-server (to mock Rest API)

If you have Java, C# etc. backend available, skip the steps 1,2, 3 and go to step 4 directly

json-server helps to mock the backend REST API and stores entered data. In this application, we demonstrate a simple use case with two operations, create new employee and fetch a list of employees.

  1. First, create db.json file, that holds employee information
{
 "employees": 
[
  {
   "id": 1,
   "firstName": "John",
   "lastName": "Reese"
  },
  {
   "id": 2,
   "firstName": "Steve",
   "lastName": "Rogers"
  }
 ]
}

2. Add json-server dependency and json-server —- watch db.json the script in package.json as shown below

"dependencies": 
{   
  ....,    
  "json-server": "^0.14.2",
  ....,
},
"scripts": 
{
  ....,
  "json-server": "json-server --watch db.json"
  ....,
}

3. start json-server by executing following command on the project root folder

json-server —-watch db.json

4. Now that, backend mock REST API server is available, let’s build the front end. In order for async to work, both the component method and service method should annotate with async and await.

async createEmployee()
{
    let url = API_URL + 'employees';
    let employee = new Employee();
    employee.id = Math.floor(Math.random() * 10000);
    employee.firstName = "John";
    employee.lastName = "McCain" + employee.id;

    //Wait for POST operation to complete then return response
    let createdEmployee=await this.employeeService.createEmployee(url, employee);
    console.log('Created Employee: '+createdEmployee);
    this.getEmployees();
}

getEmployees()
{
    let url=API_URL+'employees';
    this.employeeService.getEmployees(url).subscribe(
        data=>
        {
            this.employees=data;
        },
        error1 =>
        {
            console.log('Error');
        }
    )
}

5. createEmployee() method on component class annotated with async and employeeService.createEmployee() annotated with await. This instructs the compiler to wait for the execution of this.employeeService.createEmployee() method, then execute this.getEmployees()

5. createEmployee() method on component class annotated with async and employeeService.createEmployee() annotated with await. This instructs the compiler to wait for the execution of this.employeeService.createEmployee() method, then execute this.getEmployees()

6. When you click on CreateNew button on the HTML page, it creates a new employee with Random Id, then this.getEmployees() gets a list of employees


The code uploaded Github for reference. Download the repository and execute it.

Pavan Kumar Jadda
Pavan Kumar Jadda
Articles: 36

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.