Reputation: 2668
I'm building a react-native app where i use react-native-maps package to show markers on the map with every region by sending request to my server to get some data
<MapView
followUserLocation
initialRegion={region}
ref={(ref) => { this.mapRef = ref; }}
showsUserLocation
style={styles.map}
onRegionChange={this.onRegionChange.bind(this)}
/>
on RegionChange i send a request to my server
onRegionChange(region) {
// send my request to my server
}
but the user may make many region change in a very short time , how can i make sure that i'll get the correct response (i.e response related to the last region change ) ??
Upvotes: 1
Views: 209
Reputation: 163
There are several ways to achieve this.
Debounce
Use debounce for "onRegionChange". This way only the last change will be executed.
More info: Perform debounce in React.js
Cancelable promises
Some Promise libraries provide cancelable promises.
More info: http://bluebirdjs.com/docs/api/cancel.html
Upvotes: 1