function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SumitkSumitk 

Promise.all().catch without error in LWC?

import { LightningElement, api } from 'lwc';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import { loadStyle, loadScript } from 'lightning/platformResourceLoader';

import { createRecord, updateRecord, deleteRecord } from 'lightning/uiRecordApi';

// Static resources
import GanttFiles from '@salesforce/resourceUrl/gantt616E';

import getTasks from '@salesforce/apex/GanttCmpController.getTasks';

import {dummyData} from './dummyLoadData';

function unwrap(fromSF){
    const data = fromSF.tasks.map(a => ({
        id: a.Id,
        text: a.Name,
        start_date: a.CreatedDate,
        duration: a.Krow__Due_Date__c
    }));
    
    return { data };
}

export default class GanttCmp extends LightningElement {
    static delegatesFocus = true;

    @api height =  800;
    ganttInitialized = false;
    loadAllProjects = false;
    
    gantt;
    root;

    renderedCallback() {
        console.log('call---------->>> renderedCallback  ');
        if (this.ganttInitialized) {
            return;
        }
        this.ganttInitialized = true;

        Promise.all([
            loadScript(this, GanttFiles + '/codebase/dhtmlxgantt.js'),
            loadScript(this,'./ganttCommons'),
            loadStyle(this, GanttFiles + '/codebase/dhtmlxgantt.css'),
            loadStyle(this, GanttFiles + '/codebase/skins/dhtmlxgantt_material.css')
        ]).then(() => {
            this.initializeUI();
        }).catch((error,a,b,c,d) => {
            console.error(error);
            this.dispatchEvent(
                new ShowToastEvent({
                    title: 'Error loading Gantt',
                    message: error.message,
                    variant: 'error',
                }),
            );
        });
    }

    initializeUI(){
        console.log('call---------->>> initializeUI  ');
        this.root = this.template.querySelector('.thegantt');
        this.root.style.height = this.height + "px";

        gantt = window.Gantt.getGanttInstance();
        gantt.templates.parse_date = date => new Date(date);
        gantt.templates.format_date = date => date.toISOString();
        gantt.init(this.root);

        gantt.parse(dummyData);

        gantt.createDataProcessor({
            task: {
                create: function(data) {
                    console.log('data====>>> ',JSON.stringify(data));
                    const insert = { apiName: "Krow__Task__c", fields:{
                        Name : data.text
                       
                    }};
                    return createRecord(insert).then(res => {
                        return { tid: 1, ...res };
                    });
                },
                update: function(data, id) {
                    const update = { fields:{
                        Id: id,
                        Name : data.text
                    }};
                    return updateRecord(update).then(() => ({}));
                },
                delete: function(id) {
                    return deleteRecord(id).then(() => ({}));
                }
             }
        }).init(gantt);
    }
}

User-added imageWhen I added loadScript(this,'./ganttCommons') then caught exception without error object. I can understand what is the error. Please explain.
SwethaSwetha (Salesforce Developers) 
HI Sumit,
Promise.all is all or nothing. It resolves once all promises in the array resolve, or reject as soon as one of them rejects. In other words, it either resolves with an array of all resolved values or rejects with a single error.

Reference: https://stackoverflow.com/questions/30362733/handling-errors-in-promise-all

Promise.all runs everything inside asynchronously, which normally isn't an issue when loading scripts or styles. Can you check if there are any dependencies between the scripts in your code? You would get an undefined error when scripts refer a functionality that is not loaded yet.

If this is the case you could try chaining promises in your renderedCallback() to load things in a specified order.

Hope this helps you. Please mark this answer as best so that others facing the same issue will find this information useful. Thank you