Setting values from promise outside of promise?

I have a question I’m not sure how to ask, but I’m going to try.

I have a series of promises that load up the data for a form.

The problem is 2-fold. It takes a long time already to just load the customer and all its values, and then when I add in the invoice list, it takes even longer.

Is there a way to set a value inside a promise and then access its values outside of it to set more values? That way the form can return and then reload with the relevant data?

Here are my promises:

customer.js
(findArisId is kicked off by a search form button).

      return self.httpClient
        .fetch(url)
          .then(async (response) => {
            let aris_idObj = await response.json();
            this.customerRecord = 
              await this.getCustomerData(aris_idObj.customerData.aris_id);
            if( this.customerRecord )
            {
              this.status_code_list = await this.statusObj.getStatusCodes(this.customerRecord[0].customer.debtor_id);  
              this.selectedStatusCode = this.customerRecord[0].customer.status_code;

              this.statusSelect();
              this.worklistSelect();

              self.phone_list = this.customerRecord[3].phone;

              if (this.customerRecord[0].customer.rms_acct_id.match('Stratosphere'))
              {
                this.cellEditorParamsContactType = this.stratPhoneType;
              }
              else
              {
                this.cellEditorParamsContactType = this.phoneType;
              }

              this.initPhoneGrid(this);
              this.initAddrGrid(this);

//                  this.phoneGridOptions.api.setRowData(this.phone_list);
//                  this.phoneGridOptions.api.refreshCells();

              this.router.navigateToRoute('customer', {debtor_id: this.dbtr.debtor_id});
            }
            
            return this.customerRecord;
          });

and then this one which is the getCustomerData promise which goes to the database for information and then sets a bunch of values:

        return this.httpClient
            .fetch(this.url)
              .then(async (response) => {

                let customerRecord = await response.json();                
                
                this.parentCustNumMatch = customerRecord.customerRecord[0].customer.parent_customer_number.match( /^\S/ );
                this.credit_limit = customerRecord.customerRecord[0].customer.credit_limit;

                this.dbtr = customerRecord.customerRecord[0].customer;
                this.aging_table = customerRecord.customerRecord[5].aging_table;
                this.worklist_ids = customerRecord.customerRecord[1].worklists;
                this.selectedWorklist_id = this.dbtr.assign_id;
                this.assign_id = this.dbtr.assign_id;

                console.log('aging_table', this.aging_table);
                // set some special variables
                if ( this.dbtr.parent_customer_number && 
                  (this.dbtr.dl1 !== this.dbtr.parent_customer_number) &&
                  this.dbtr.parent_credit_limit )
                {
                  this.parent_credit_limit = commas( this.dbtr.parent_credit_limit );
                }

                if (this.dbtr)
                {
                  // get invoices
                  this.invoiceList = await this.invoiceListObj.getInvoiceList(this.dbtr.debtor_id);
                  console.log('invoiceList', this.invoiceList);
                  
                  this.dbtr.orig_next_date = this.dbtr.next_contact_date;
                  this.reportAsLink += this.dbtr.debtor_id;
                  this.offset = this.dbtr.offset;
                  this.plusDate = this.dbtr.plus_date;

                  this.aging = this.dbtr.aging;

                  this.dbtr.current_balance = parseFloat(this.dbtr.current_balance).toFixed(2);
                  var dateArray = this.dbtr.next_contact_date.split('-');

                  var newDate = dateArray[1] + '/' + dateArray[2] + '/' + dateArray[0];

                  //this.dbtr.next_contact_date = newDate;

//                  console.log('next_contact_date', this.dbtr.next_contact_date);
                  var risk = this.dbtr.risk[0].toUpperCase() + this.dbtr.risk.substring(1);
                  this.dbtr.risk = risk;

                  // set booleans for group_checkboxes
                  this.credit_hold = this.dbtr.credit_hold === 1 ? true : false;
                  this.watch_list = this.dbtr.watch_list === 1 ? true : false;
                  this.do_not_call = this.dbtr.do_not_call === 1 ? true : false;

//                  console.log('max_offset', this.dbtr.max_offset);

//                  var maxOffset = this.dbtr.max_offset.match( /(\d+)/ );    
//                  this.maxOffsetDate = new Date();
//                  var offset = parseInt(maxOffset[0]);
              
//                  this.maxOffsetDate.setDate(this.maxOffsetDate.getDate() + offset);
              
//                  this.dateOptions = 
//                    {
//                      format:'YYYY-MM-DD',
//                      maxDate: this.maxOffsetDate,
//                      daysOfWeekDisabled: [0,6],                  
//                    };

                }

                if( customerRecord.customerRecord[0].credit_limit )
                {
                  this.numeric_cred = customerRecord.customerRecord[0].credit_limit.replace(/\D/g, '');
                }
                else
                {
                  this.numeric_cred = 0;
                }

                if (this.dbtr)
                {
                  self.startClock();
//                  console.log( 'httpClient', this.httpClient );
                  
                  self.checkFlags = await
                    check_flag(self.flags, self.dbtr.debtor_id, 
                        self.user, self.httpClient);
                }
                
                for ( var f = 0; f < self.checkFlags.flags.length; f++)
                {
//                  console.log( 'f', self.checkFlags.flags[f]);

                  var fValues = self.checkFlags.flags[f];

                  if ( fValues.field.match('read_only'))
                  {
                    this.read_only = fValues.flag;
                  }
                  
                  if ( fValues.field.match('read_docs'))
                  {
                    this.read_docs = fValues.flag;
                  }
                  
                  if ( fValues.field.match('set_watch_list'))
                  {
                    this.set_watch_list = fValues.flag;
//                    console.log('watch_list', this.set_watch_list);
                  }

                  if ( fValues.field.match('set_priority_followup'))
                  {
                    this.set_priority_followup = fValues.flag;
                  }
                  
                  if ( fValues.field.match('change_office'))
                  {
                    this.change_office = fValues.flag;
                  }
                  
                  if ( fValues.field.match('change_worklist'))
                  {
                    this.change_worklist = fValues.flag;
                  }

                  if ( fValues.field.match('modify_report_as'))
                  {
                    this.modify_report_as = fValues.flag;
                  }

                  if ( fValues.field.match('maanger'))
                  {
                    this.manager = fValues.flag;
                  }

                  if ( fValues.field.match('temp_hold'))
                  {
                    this.temp_hold = fValues.flag;
                  }

                  if ( fValues.field.match('delete_contact_information'))
                  {
                    this.delete_contact_information = fValues.flag;
                  }
                }

                this.phone_list = customerRecord.customerRecord[3].phone;
                
                if ( typeof(this.delete_phone_item) != undefined && this.delete_phone_item != null)
                {
                  this.phoneGridOptions.api.setRowData(this.phone_list);
                  this.phoneGridOptions.api.refreshCells();
                }

                this.addr_list = customerRecord.customerRecord[4].address;
                
//                console.log('addr_list', this.addr_list);

                if ( typeof(this.delete_address_item) != undefined && this.delete_address_item != null)
                {
                  this.addrGridOptions.api.setRowData(this.addr_list);
                  this.addrGridOptions.api.refreshCells();
                }

                return customerRecord.customerRecord;
              });

What I would like to do is return the object, and start loading up the form. Right now this alone takes up to 30 seconds to return, and I need to be able to load notes and an invoice list in a reasonable amount of time.

1 Like

Hi @rhysshadow , my general advice for that is to not await the fetch in activation phase. Not that if you return the promise from the “xactivate” methods then Aurelia will wait for that promise to get settled.

Rather you can save that promise to a class property and use that that promise in your view such a way that until the promise is resolved, a wait message/spinner is shown.

1 Like