---
schema_version: "1"
id: eed222fe259a6570d62cfe381621b00344cb1d00c3e5114bbf40f53c8f0a24af
title: getCallback()
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/platform/aura-platform/guides/aura/ref-jsapi/ref-jsapi-dollara-getcallback.md
canonical_url: https://developer.salesforce.com/docs/platform/aura-platform/guide/ref-jsapi-dollara-getcallback.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: 15f45aeba644e30c41cd0cdcae295c4e6af547cd7d91923e9e0537d4a8431526
products:
  - Lightning Aura Components Developer Guide
  - Platform Developer Content
---

# getCallback()

Use `$A.getCallback()` to wrap any code that modifies a component outside the normal rerendering lifecycle, such as in a `setTimeout()` call. The `$A.getCallback()` call ensures that the framework rerenders the modified component and processes any enqueued actions.

Don't use `$A.getCallback()` if your code is executed as part of the framework's call stack. For example, your code is handling an event or in the callback for an Apex controller action.

Run async operations with a `$A.getCallback()` wrapper. For example, use `setTimeout()` and `setInterval()` with `$A.getCallback()`. Use Promise resolve or reject handlers with `$A.getCallback()`.

When using `$A.getCallback(function callback)` with a Promise, the function runs after the Promise resolves. For example:

```javascript
({
    getUser : function(component, event, helper) {
        
        // Call helper to get the Promise object
        var userPromise = helper.fetchUserData();
        
        // Set the Promise object into an attribute (v.userPromise)
        component.set("v.userPromise", userPromise);
        
        // Use .then() with the Promise that's retrieved via component.get()
        // The Promise resolves and its result is passed to the function
        component.get("v.userPromise").then(
            
            $A.getCallback(function(result) { 
                
                // 'result' is returned object from the helper
                var userName = result.name; 
                var userId = result.id;
                
                // Use the data to update a component attribute
                component.set("v.userName", userName);
            })
        ).catch($A.getCallback(function(error) {
            // Handle any potential errors during the promise resolution
            component.set("v.userName", "Error fetching data.");
            console.error("Promise rejected:", error);
        }));
    }
})
```

## Signature

`getCallback (function callback)`

## Parameters

**_callback_:** Type: `function`. The method to call after establishing an Aura context.

## Sample Code

Use `$A.getCallback()` with component validity check.

```javascript
window.setTimeout(
    $A.getCallback(function() {
        if(cmp.isValid())
        cmp.set("v.value", data);
    }), 5000
);
```

Use Promise handling with Aura lifecycle management.

```javascript
promise.then($A.getCallback(function(result) {
    if(cmp.isValid())
    helper.process(result); 
    }
));
```

**See Also**  


-   [Modifying Components Outside the Framework Lifecycle](https://developer.salesforce.com/docs/platform/aura-platform/js-cb/js-cb-mod-ext-js.md)

