Quantcast
Channel: Ionic Framework - Ionic Forum
Viewing all articles
Browse latest Browse all 49526

Promise returning Subscription, how to wait for data before proceding

$
0
0

@leonardofmed wrote:

I have a function that get some data from DB, store in localStorage and then do something with it. To get the data I’m using the Angular’s HttpClient, which uses Observables in its methods. The problem I’m facing is that when processData() is fired, the localStorage was not populated yet.

Here is how I’m doing the coding so far:

mainFunction(): Promise<void> {
	return this.getData().then(() => {
  		return this.processData().then(() => {
  			//...
  		})
  	})
}

// Get data from DB and then store in localStorage
getData(): Promise<Subscription> {
	// Declare some data to send
	//...

	return this.http.post(myLink, myArray, httpOptions)
	.subscribe(res => {
		// Store retrieved data in localStorage
		return this.storeData(res);
	});
}

I can’t just get the data from getData() and pass to processData, for some other reasons I need to update the localStorage before proceed. I believe this situation is happening because I’m returning a Subscription to processData() and not the updated localStorage data, which is my objective. I tried to do this in some ways, as stated here using map(), but this generate some other errors. Is there other method that I can use to acomplish what I want?

Posts: 3

Participants: 2

Read full topic


Viewing all articles
Browse latest Browse all 49526

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>