Invoking Continuation class from Lightning Component

Asynchronous callouts (also referred to as Apex continuations) allow you to invoke long-running services without tying up server resources and running into Apex concurrency limits. An asynchronous callout made with a continuation doesn’t count toward the Apex limit of 10 synchronous requests that last longer than five seconds. Therefore, you can make more long-running callouts and integrate your component with a complex back-end API.

Natively salesforce didn’t allow Lightning component (Aura and LWC) to invoke Continuation apex. It was only supported from Visualforce pages. But with Summer ’19 release, an Aura component or a Lightning web component can use the Continuation class in Apex to make a long-running request to an external web service. Isn’t that good news !!!

This wonderful blog post describes still how you can invoke apex continuation from lightning component which was not yet supported natively by salesforce. This requires the usage of Visualforce page embedded as an iframe in the Lightning component. This VF page will then do the magic to invoke Continuation apex and post the response back to Lightning component.

But with the new release, you can easily invoke the Continuation apex from Lightning component. Below is the example with the details. But before starts, you need to register for the Summer 19 pre release org.

Here is the apex class that use the continuation and make the long running callout.

@AuraEnabled(continuation=true cacheable=true)

    global static Object getProduct(Integer productId, Integer latency){

 // Make an HTTPRequest as we normally would

        // Remember to configure a Remote Site Setting for the service!

        String url = '' + productId + '?latency=' + latency;

        HttpRequest req = new HttpRequest();


        // Create a Continuation for the HTTPRequest        
       //Timeout in seconds, 60 is limit
        Continuation con = new Continuation(60);

        con.state = con.addHttpRequest(req);

        con.continuationMethod = 'callback';     
        // Return it to the system for processing

        System.debug('returned from server'+con);

        return con;


Use continuation=true and cacheable=true with AuraEnabled annotation to invoke it from lightning component. You have to define the call back method which process the response asynchronously.


    global static Object callback(Object state) {
        HttpResponse response = Continuation.getResponse((String)state);
        Integer statusCode = response.getStatusCode();
        if (statusCode >= 2000) {
               return 'Continuation error: ' + statusCode;
        System.debug('response @@'+response.getBody()+'Status code'+statusCode);
        return response.getBody();

Use @AuraEnabled(cacheable=true) annotation with the callback method to access the response from the Lightning component.

In this example, i have used service where you can set the latency of the transaction. So that you can experience the actual long running query behaviour.

Here is the full code of the apex class and aura component.

Please see below the video of the working example. You can see when the latency is 2000 , long running query takes longer time time than the next callout where the latency is 20.

Resource: Salesforce Release note summer 19 – Long running queries from Lightning.

Use Lightning Web Components in Visualforce Pages

With Summer ’19 release, salesforce has introduced the use of lwc in visualforce pages. It will be used in same way like any aura components before.

Before starts, please sign up for pre-release developer edition.

Steps to add the Lightning web component in the visualforce page.

  1. Add the Lightning Web Components for Visualforce JavaScript library to your Visualforce page using the <apex:includeLightning/> component.

  <apex:includeLightning />



2. Create and reference a standalone Aura app that declares your component dependencies.

<aura:application extends=”ltng:outApp” access=”GLOBAL”>
<aura:dependency resource=”c:demolwcforLighntingout” />
<aura:dependency resource=”markup://force:*” type=”EVENT”/>

Below is the lwc definition

<lightning-card title=”Hello” icon-name=”custom:custom14″>
<div class=”slds-m-around_medium”>
Hello, {greeting}!

import { LightningElement,api } from ‘lwc’;

export default class DemolwcforLighntingout extends LightningElement
@api greeting ;

3. Write a JavaScript function that creates the component on the page using $Lightning.createComponent().

  <apex:includeLightning />
  <div id=”container”>

$Lightning.use(“c:lwcContainerApp”, function()
console.log(‘component created’);

Note: You can pass arguments to the lightning web components. In lwc, that property must be publicly accessible. So marking it with @api.

Use namespace:camelCaseComponentName naming convention to reference the Lightning web component.