Skip to main content
2 of 2
added 113 characters in body

Callout not happening from Continuations class(LWC)

I am trying to do a callout on a button click from my LWC comp using apex Continuations. In the debug log, I see no callout happened. But when I do the callout using setTimeout(), then its working fine. Can someone please help me where I am doing wrong? I have used the below code.

HTML: <lightning-button variant="brand" label="Fetch Contacts" onclick={findContacts} ></lightning-button>

import { LightningElement,track } from 'lwc';
import startContinuation from '@salesforce/apexContinuation/ContinuationCustom.startContinuation';

export default class CustomContactScreen extends LightningElement {

    findContacts(){
         
        startContinuation().then(result => {      
            console.log('result='+JSON.stringify(result))      
        })
        .catch(error => {
           
        });  
    }
}

apex class:

public with sharing class ContinuationCustom{
    // Test variable used for holding a reference to the Continuation Request
    @TestVisible
    private static String continuationLabel;
    // Test variable used for holding the value of the Continuation.state attribute
    @TestVisible
    private static String continuationState;
    
    // The function that fires the continuation
    @AuraEnabled(continuation=true cacheable=true)
    public static Object startContinuation() {
        // Create the callout Request
        HttpRequest req = new HttpRequest();
        req.setMethod('GET');
        req.setEndpoint('URL for callout');

        // Create the continuation with a 40s timeout
        Continuation con = new Continuation(40);
        con.ContinuationMethod = 'continuationCallback';
        
        // Store the reference to the HttRequest and make it accessible for a test-context
        continuationLabel = con.addHttpRequest(req);
        
        // Store data to be sent to the callback function
        continuationState = 'Some data here...';
        con.state = continuationState;
        System.debug('req=='+req+'===con=='+con);
        return con;
    }

    // The function that processes the continuation callback
    @AuraEnabled(cacheable=true)
    public static Object continuationCallback(List<String> labels, Object state) {
        HttpResponse response = Continuation.getResponse(labels[0]);
        Integer statusCode = response.getStatusCode();
        // TODO: handle the different status codes
        
        return response.getBody();
    }
}