Ahmed Ali
Ahmed Ali

Reputation: 2668

handling multi requests in react-native and redux and react-native-maps

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

Answers (1)

Lalli Nuorteva
Lalli Nuorteva

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

Related Questions