---
schema_version: "1"
id: bb3f455eea23f63eef2743b98353f72960ecf81bfd0d13265147b06ab4283774
title: Process for Using Asynchronous Callouts
collection: legacydeveloper
version: current
locale: en-us
portal_url: https://docs.salesforce.com/legacydeveloper/current/en-us/apexcode/apex_continuation_process.md
canonical_url: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_continuation_process.htm
source_site: developer.salesforce.com
content_type: documentation
content_hash: 13e69446b51252bccbd50320a322a2c5300573e3c012407a3a91b7ab65dd095b
description: To use asynchronous callouts, create a Continuation object in an action method of a controller, and implement a callback method.
---
# Process for Using Asynchronous Callouts

To use asynchronous callouts, create a Continuation object in an action method of a controller, and implement a callback method.

## Invoking an Asynchronous Callout in an Action Method

To invoke an asynchronous callout, call the external service by using a Continuation instance in your Visualforce action method. When you create a continuation, you can specify a timeout value and the name of the callback method. For example, the following creates a continuation with a 60-second timeout and a callback method name of processResponse.

Continuation cont = new Continuation(60);
cont.continuationMethod = 'processResponse';

Next, associate the Continuation object to an external callout. To do so, create the HTTP request, and then add this request to the continuation as follows:

String requestLabel = cont.addHttpRequest(request);

![Note](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/images/icon_note.png)

#### Note

This process is based on making callouts with the HttpRequest class. For an example that uses a WSDL-based class, see [Making an Asynchronous Callout from an Imported WSDL](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_continuation_callout_soap.htm "In addition to HttpRequest-based callouts, asynchronous callouts are supported in Web service calls that are made from WSDL-generated classes. The process of making asynchronous callouts from a WSDL-generated class is similar to the process for using the HttpRequest class.").

The method that invokes the callout (the action method) must return the Continuation object to instruct Visualforce to suspend the current request after the system sends the callout and waits for the callout response. The Continuation object holds the details of the callout to be executed.

This is the signature of the method that invokes the callout, where calloutActionMethodName is a placeholder value. The Object return type represents a Continuation.

public Object calloutActionMethodName()

## Defining a Callback Method

The response is returned after the external service finishes processing the callout. You can specify a callback method for asynchronous execution after the callout returns. This callback method must be defined in the controller class where the callout invocation method is defined. You can define a callback method to process the returned response, such as retrieving the response for display on a Visualforce page.

The callback method doesn’t take any arguments and has this signature, where callbackMethodName is a placeholder value:

public Object callbackMethodName()

The Object return type represents a Continuation, a PageReference, or null. To render the original Visualforce page and finish the Visualforce request, return null in the callback method.

If the action method uses JavaScript remoting (is annotated with @RemoteAction), the callback method must be static and has the following supported signatures, where callbackMethodName, labels, and state are placeholder values.

public static Object callbackMethodName(List< String\> labels, Object state)

Or:

public static Object callbackMethodName(Object state)

The labels parameter is supplied by the system when it invokes the callback method and holds the labels associated with the callout requests made. The state parameter is supplied by setting the [Continuation.state](https://developer.salesforce.com/docs/atlas.en-us.264.0.apexref.meta/apexref/apex_class_System_Continuation.htm#apex_System_Continuation_state) property in the controller.

This table lists the return values for the callback method. Each return value corresponds to a different behavior.

<table class="featureTable sort_table" summary="">
          <caption><span class="tablecap">Table 1. Possible Return Values for the Callback Method</span></caption>


          <thead class="thead sorted" align="left">
            <tr>
              <th class="featureTableHeader  " id="d21985e188">Callback Method Return Value</th>

              <th class="featureTableHeader  " id="d21985e191">Request Lifecycle and Outcome</th>

            </tr>

          </thead>

          <tbody class="tbody">
            <tr>
              <td class="entry" headers="d21985e188" data-title="Callback Method Return Value"><samp class="codeph apex_code"><span class="keyword">null</span></samp></td>

              <td class="entry" headers="d21985e191" data-title="Request Lifecycle and Outcome">The system finishes the Visualforce page request and renders the original
                Visualforce page (or a portion of it).</td>

            </tr>

            <tr>
              <td class="entry" headers="d21985e188" data-title="Callback Method Return Value"><samp class="codeph apex_code">PageReference</samp></td>

              <td class="entry" headers="d21985e191" data-title="Request Lifecycle and Outcome">The system finishes the Visualforce page request and redirects to a new
                Visualforce page.<p class="p">(Use query parameters in the <samp class="codeph apex_code">PageReference</samp> to pass the results of the <samp class="codeph apex_code">Continuation</samp> to the new page.)</p>
              </td>

            </tr>

            <tr>
              <td class="entry" headers="d21985e188" data-title="Callback Method Return Value"><samp class="codeph apex_code">Continuation</samp></td>

              <td class="entry" headers="d21985e191" data-title="Request Lifecycle and Outcome">The system suspends the Visualforce request again and waits for the response of
                a new callout. Return a new <samp class="codeph apex_code">Continuation</samp> in
                the callback method to chain asynchronous callouts.</td>

            </tr>

          </tbody>

        </table>

![Note](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/images/icon_note.png)

#### Note

If the continuationMethod property isn’t set for a continuation, the same action method that made the callout is called again when the callout response returns.

#### See Also

- [_Apex Reference Guide_: Continuation Class](https://developer.salesforce.com/docs/atlas.en-us.264.0.apexref.meta/apexref/apex_class_System_Continuation.htm "Apex Reference Guide: Continuation Class - HTML (New Window)")