2

I'm implementing a method in a service that is used to initialize indexedDB Data. The method should call a webapi in method Ordini_Sincronizza, get the results and process the indexed db.

The code is this:

  SyncCustomer(codCli: string): Observable<boolean>
  {
    this.mdService.Ordini_Sincronizza(codCli).subscribe(
      data => {

        this._DBService.Configurazione.clear().then(
          act => {
            let itm = new ConfigurazioneFlat();

            itm.codIva = data.configurazione.codIva;
            itm.notOrd = data.configurazione.notOrd;
            itm.ordini_nota = data.ordiniArticoli.nota;

            this._DBService.Configurazione.add(itm);
            console.log('INITIALIZED: Configurazione_Flat');
          }
        );

        this._DBService.Configurazione_Sconti.clear().then(
          act => {
            for (let itm of data.configurazione.sconti) {
              this._DBService.Configurazione_Sconti.add(itm);
            }
            console.log('INITIALIZED: Configurazione_Sconti');
          }
        );

        this._DBService.Configurazione_TipiOmaggio.clear().then(
          act => {
            for (let itm of data.configurazione.tipiOmaggio) {
              this._DBService.Configurazione_TipiOmaggio.add(itm);
            }
            console.log('INITIALIZED: Configurazione_TipiOmaggio');
          }
        );

        this._DBService.Listino_Testata.clear().then(
          act => {
            for (let l of data.listini) {
              this._DBService.Listino_Testata.add(l.testata);
            }
            console.log('INITIALIZED: Listino_Testata');
          }
        );

        return Observable.of(true);
      },
      error => {
        console.log(error);
        return Observable.of(false);
      });

    return Observable.of(null);   

  }

The method is placed inside the SyncService ( typescript service ) and returns true if all works fine or false in case of error.

Now I want to use this service method in a component, I'm doing this:

  DownloadData()
  {
    this.syncService.SyncCustomer(this.p_codCli).subscribe(
      data => {
        alert('ok');        
      },
      error => {
        alert('err');
        console.log(error);

      });
  }

But the strange behavior that I'm facing is that I get immediately the alert('ok') and after I receive the logs message in console.

I think its a sync/async calls problem for sure but I don't know how to manage it.

Thanks to support

Aluan Haddad
  • 29,886
  • 8
  • 72
  • 84
DarioN1
  • 2,460
  • 7
  • 32
  • 67
  • 1
    So you want to receive first console logs and after everything is done call `alert('ok')`? – mxr7350 Sep 28 '17 at 08:28
  • 1
    Possible duplicate of [How do I return the response from an Observable/http/async call in angular2?](https://stackoverflow.com/questions/43055706/how-do-i-return-the-response-from-an-observable-http-async-call-in-angular2) – Aluan Haddad Sep 28 '17 at 08:29
  • mxr7350, the execution I expect is to get the alert('ok') once time all the clear/insert commands are executed – DarioN1 Sep 28 '17 at 08:37
  • 1
    try make use of forkJoin or promise.all to simplify the code – Fan Cheung Sep 28 '17 at 08:49
  • Can you provide me an example ? – DarioN1 Sep 28 '17 at 08:51

0 Answers0