1

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 ) ??

Ahmed Ali
  • 2,574
  • 2
  • 23
  • 38

1 Answers1

1

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

Community
  • 1
  • 1