javascript - Observable subscribe timing issue -


i have service in component, service has method returns string value server.

service:

getorderbarcode(): observable<string> {     return this.http.get(this.baseurl + 'barcode/getbarcode')         // .map(this.extractdata);         .map(res => res string ) } 

component:

let barcodeservice = this.barcodeservice.getorderbarcode(); debugger; barcodeservice.subscribe(result => order.orderbarcode = result); console.log(order.orderbarcode); // rest of logic 

however, when .subscribe called not binding value @ point, after debugging found applies value after executing in class, how can around issue?

update based on comments , link question

service method:

getsomething(callback: (data) => void) {     debugger;     return this.http.get(this.baseurl + 'barcode/getbarcode')         // .timeout(1000)         // .map(this.extractdata);         .map(res => res string) } 

component:

let = this.barcodeservice.getsomething((data) => {     debugger;     console.log(data); }); 

this debugger not hit...

your sample code

barcodeservice.subscribe(result => order.orderbarcode = result); console.log(order.orderbarcode); 

fix

use topromise , await result (or use then).

more: https://www.learnrxjs.io/operators/utility/topromise.html


Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -