Ionic How to get out of a routine

I am making an application in ionic but I need that when the application enters a segment of the code it goes out of the routine and does not continue executing the code

in the code that I want this to do that is the one that is highlighted in bold or appears with two err => {console.log (err); return; , I want that when you enter that code segmendo the application does not continue executing the rest

console.log('Los pedidos estan quiero que sincronicen');

  var headers = new Headers();//
  headers.append('Content-Type', 'application/json' );
  const requestOptions = new RequestOptions({ headers: headers })
  let url = URL_SERVICIOS+"pedidos/post/", JSON.stringify( this.postData),requestOptions)
  .map(res => res.json()).subscribe (data =>{

    console.log('Los pedidos estan quiero que sincronicen2');

  **err => {

  console.log("numero de pedidos " +  data.rows.item(i).PedCodigo );

}, (error) => {

First, there is a lot of needless junk here obscuring the main issue. You do not need custom Headers. You do not need to be manually setting Content-Type. You do not need to be manually calling JSON.stringify. In fact, doing any of these things messes up what should be a very simple operation, initiating a cascade of having do do the rest of them.

Angular’s HttpClient is pretty smart. If you pass an object as the second argument to post, Content-Type will be automatically set to application/json. It also unmarshals on return. So your entire HTTP call can be simplified to just this: + "pedidos/post/", this.postData).subscribe

The presence of resolve worries me greatly, because I fear you’re doing new Promise up above the part you pasted. That’s also unneeded and causing unwanted complexity.

So here is the fundamental problem: you are trying to program imperatively in a reactive environment. You have to completely change the way you think about control flow when writing web applications. The entire premise of this thread is basically asking "how do I do a goto", but you are not in charge of what is currently being executed, and never will be.

The fragment of code that you starred is not connected in any way to the calls to PedidosDetalleSincronizar and CambiarEstatusSincronizado. It cannot affect anything outside of the {} block that encloses it. It will run when the HTTP request has resolved, which is an unknown point in the future, unrelated to anything else in this function.

So, what to do?

First of all, if this code is in a page, get it out of there and put it into a service. I don’t see how Pedidos is populated, and that seems to be the most important part of this code. I would rewrite this function to return Observable<TypeOfPedidos>, using map operators to do the PedidosDetalleSincronizar and CambiarEstatusSincronizado work, assuming that those functions are somehow transforming the HTTP results into Pedidos.

Try to rewrite this code in a completely functional manner. This means not using any external state, which in turn means no this at all. No this.postData, no this.PedidosDetalleSincronizar, no this.CambiarEstatusSincronizado. All of that stuff must be put into either this function itself, or any helpers used must be static. While this isn’t strictly required, it will help you naturally avoid a class of bugs that is seemingly plaguing you.

I know this is disorientating and frustrating. You’re probably tempted to ignore everything I’m saying and just wait for another answer that doesn’t require such drastic change to probably everything in your app. I certainly was when I was struggling with learning to program reactively. I really am trying to make your life better, even if it seems like just the opposite at the moment.

1 Like