import ballerina/io;
import ballerina/runtime;
import ballerina/http;
import ballerina/mime;int count;endpoint http:Client clientEndpoint { targets:[{url: "https://postman-echo.com" }] };function main(string[] args) {
  future<int> f1 = async sum(40, 50);
  int result = square_plus_cube(f1);
  io:print("SQ + CB = ");
  io:println(result);
  future f2 = async countInfinity();
  runtime:sleepCurrentWorker(1000);
  io:println(f2.isDone());
  io:println(f2.isCancelled());
  boolean cancelled = f2.cancel();
  io:println(cancelled);
  io:print("Counting done in one second: ");
  io:println(count);
  io:println(f2.isDone());
  io:println(f2.isCancelled());
  
  http:Request req = new;
    future<http:Response|http:HttpConnectorError> f3 = async clientEndpoint -> get("/get?test=123", req);
  io:println(sum(25, 75));
  io:println(f3.isDone());
  var response = await f3;
  match response {
    http:Response resp => {
      io:println("HTTP Response: ");
      var msg = resp.getJsonPayload();
      match msg {
        json jsonPayload1 => {
          io:println(jsonPayload1);
        }
        http:PayloadError payloadError1 => {
          io:println(payloadError1.message);
        }
      }
    }
    http:HttpConnectorError err => { io:println(err.message); }
  }
  io:println(f3.isDone());
}function sum(int a, int b) returns int {
  return a + b;
}function square(int n) returns int {
  return n * n;
}function cube(int n) returns int {
  return n * n * n;
}function square_plus_cube(future<int> f) returns int {
  worker w1 {
    int n = await f;
    int sq = square(n);
    sq -> w2;
  }
  worker w2 {
    int n = await f;
    int cb = cube(n);
    int sq;
    sq <- w1;
    return sq + cb;
  }
}function countInfinity() {
  while (true) {
    count++;
  }
}

Async

Async functionality in Ballerina is a way to execute operations in an asynchronous manner by returning a future. A future can be used to check the running status of an execution, or else, cancel the operation if needed.

import ballerina/io;
import ballerina/runtime;
import ballerina/http;
import ballerina/mime;
int count;
endpoint http:Client clientEndpoint { targets:[{url: "https://postman-echo.com" }] };
function main(string[] args) {
  future<int> f1 = async sum(40, 50);

Asynchronously call the function named ‘sum’.

  int result = square_plus_cube(f1);
  io:print("SQ + CB = ");
  io:println(result);

You can pass around the value of the ‘future’ variable and call its results later.

  future f2 = async countInfinity();
  runtime:sleepCurrentWorker(1000);

Call the ‘countInfinity’ function, which runs forever in asynchronous mode.

  io:println(f2.isDone());

Check whether the function call is done.

  io:println(f2.isCancelled());

Check whether someone cancelled the asynchronous execution.

  boolean cancelled = f2.cancel();
  io:println(cancelled);
  io:print("Counting done in one second: ");
  io:println(count);
  io:println(f2.isDone());
  io:println(f2.isCancelled());
  

Cancel the asynchronous operation.

  http:Request req = new;
  

async action call

  future<http:Response|http:HttpConnectorError> f3 = async clientEndpoint -> get("/get?test=123", req);
  io:println(sum(25, 75));
  io:println(f3.isDone());
  var response = await f3;
  match response {
    http:Response resp => {
      io:println("HTTP Response: ");
      var msg = resp.getJsonPayload();
      match msg {
        json jsonPayload1 => {
          io:println(jsonPayload1);
        }
        http:PayloadError payloadError1 => {
          io:println(payloadError1.message);
        }
      }
    }
    http:HttpConnectorError err => { io:println(err.message); }
  }
  io:println(f3.isDone());
}
function sum(int a, int b) returns int {
  return a + b;
}
function square(int n) returns int {
  return n * n;
}
function cube(int n) returns int {
  return n * n * n;
}
function square_plus_cube(future<int> f) returns int {
  worker w1 {
    int n = await f;
    int sq = square(n);
    sq -> w2;
  }
  worker w2 {
    int n = await f;
    int cb = cube(n);
    int sq;
    sq <- w1;
    return sq + cb;
  }
}
function countInfinity() {
  while (true) {
    count++;
  }
}
$ ballerina run async.bal
SQ + CB = 737100
false
false
true
Counting done in one second: 17839763
true
true
100
false
HTTP Response: 
{"args":{"test":"123"},"headers":{"host":"postman-echo.com","accept-encoding":"deflate, gzip","user-agent":"ballerina/0.9xx","x-forwarded-port":"443","x-forwarded-proto":"https"},"url":"https://postman-echo.com/get?test=123"}
true
17839763