Newer Version Available

This content describes an older version of this product. View Latest

JavaScript Promises

Methods in the Lightning Console JavaScript API return results using promises.

Examples in this guide don’t include the $A.getCallback() wrapper because the Lightning Console JavaScript API returns promises that already include the $A.getCallback() wrapper around callback functions. This is reflected in the sample code throughout this guide.

Note

Use JavaScript Promises in LWC

This example uses the openTab() to get the tab ID of the focused tab. Then the function calls focusTab() with the tabId that’s returned by the openTab() method.

1import { LightningElement } from 'lwc';
2import { openTab, focusTab } from 'lightning/platformWorkspaceApi';
3
4export default class MyComponent extends LightningElement {
5    focusNewTab(event) {
6        openTab({
7            url: '/lightning/r/Account/001R0000003HgssIAC/view',
8            label: 'Global Media'
9        }).then((tabId) => {
10            focusTab(tabId);
11        }).catch((error) => {
12            console.log(error);
13        });   
14    }
15}

You can also simplify the JavaScript promise as follows.

1openTab({
2    url: '/lightning/r/Account/001R0000003HgssIAC/view',
3    label: 'Global Media',
4    focus: true
5}).catch((error) => {
6    console.log(error);
7});

Use JavaScript Promises in Aura

Here’s the same example as using JavaScript promises in LWC, written for Aura components.

1({
2    focusNewTab : function(component, event, helper) {
3        var workspaceAPI = component.find("workspace");
4        workspaceAPI.openTab({
5            url: '#/sObject/001R0000003HgssIAC/view',
6            label: 'Global Media'
7        }).then(function(response) {
8            workspaceAPI.focusTab({tabId : response});
9       })
10        .catch(function(error) {
11            console.log(error);
12        });
13    }
14})