Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

axios cancel previous request

let cancelToken
2const handleSearchChange = async e => {
3  const searchTerm = e.target.value
4
5  //Check if there are any previous pending requests
6  if (typeof cancelToken != typeof undefined) {
7    cancelToken.cancel("Operation canceled due to new request.")
8  }
9
10  //Save the cancel token for the current request
11  cancelToken = axios.CancelToken.source()
12
13  try {
14    const results = await axios.get(
15      `http://localhost:4000/animals?q=${searchTerm}`,
16      { cancelToken: cancelToken.token } //Pass the cancel token to the current request
17    )
18    console.log("Results for " + searchTerm + ": ", results.data)
19  } catch (error) {
20    console.log(error)
21  }
22}
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript take last n elements of array 
Javascript :: vuejs props declare prop with multiple types 
Javascript :: access selected option in jquery 
Javascript :: how to store array of object in local storage 
Javascript :: array to string javascript 
Javascript :: change index array javascript 
Javascript :: express response setTimeout 
Javascript :: javascript simulate click on element 
Javascript :: Take a Ten Minute Walk js 
Javascript :: javascript, digit thousand formatting, number formating js, regexp, number comma seperation js 
Javascript :: javascript dynamic import 
Javascript :: convert days into year month 
Javascript :: Javascript how to differentiate single click event and double click event 
Javascript :: pass id to reactjs routes 
Javascript :: npm adm-zip 
Javascript :: how to read all files in a folder in node js 
Javascript :: sanitizing user input javascript 
Javascript :: javascript check image src 
Javascript :: add multiple event listeners 
Javascript :: javascript console.log alternative 
Javascript :: javascript mouse up mouse down 
Javascript :: use eslint in vscode 
Javascript :: firestore javascript delete document 
Javascript :: regex for username 
Javascript :: how to get single element from nested array mongoose 
Javascript :: es ignore 
Javascript :: how to remove minutes with moment js 
Javascript :: javascript add text to li 
Javascript :: react input number validation 
Javascript :: @react-google-maps/api npm 
ADD CONTENT
Topic
Content
Source link
Name
8+9 =