angular - Angular2 : GET request not running syncronously -
i have problem http request.. code not running synchronously.. below example code
this.jilidservice.getdblist().subscribe( data => { (let = 0; < data.length; i++) { this.data = data; this.db_name[i] = this.data[i] this.dbname = this.db_name[i] console.log("1") } }, ); this.jilidservice.getfilelist(this.dbname).subscribe( data1 => { (let = 0; < data1.length; i++) { this.data = data1; this.filename = data1[i] console.log("2") } }, );
when run code result got console log is:-
2 1
the result want
1 2
thank in advance
yes, correct. http calls asynchronous nature. how work. issue http request , @ future point in time receive http response. http has been way.
the method passed subscribe callback function called when response received.
one way resolve issue put second call within callback function of first. this:
this.jilidservice.getdblist().subscribe( data => { (let = 0; < data.length; i++) { this.data = data; this.db_name[i] = this.data[i] this.dbname = this.db_name[i] console.log("1") } this.jilidservice.getfilelist(this.dbname).subscribe( data1 => { (let = 0; < data1.length; i++) { this.data = data1; this.filename = data1[i] console.log("2") } }, ); }, );
another option use switchmap suggested rahul. can find example of here:
how wait first observable finish before executing others in parallel using rxjs
Comments
Post a Comment