diff --git a/sample-app/src/components/SearchBar.tsx b/sample-app/src/components/SearchBar.tsx index 1a38fcb7..d86ddb85 100644 --- a/sample-app/src/components/SearchBar.tsx +++ b/sample-app/src/components/SearchBar.tsx @@ -32,12 +32,12 @@ export default function SearchBar({ const isLoading = useAnswersState(state => state.searchStatus.isLoading); const [ autocompleteResponse, - latestAutocompleteResponseRef, + responseToLatestRequestRef, executeAutocomplete ] = useAutocomplete(isVertical); async function executeQuery () { - await latestAutocompleteResponseRef.current; + await responseToLatestRequestRef.current; if (autocompleteResponse?.inputIntents.includes(SearchIntent.NearMe)) { const position = await SearchHandler.getUserLocation(geolocationOptions); answersActions.setUserLocation({ diff --git a/sample-app/src/hooks/useAutocomplete.tsx b/sample-app/src/hooks/useAutocomplete.tsx index 37dfdf87..f8aa5200 100644 --- a/sample-app/src/hooks/useAutocomplete.tsx +++ b/sample-app/src/hooks/useAutocomplete.tsx @@ -7,21 +7,21 @@ export function useAutocomplete( const answersActions = useAnswersActions(); const autocompleteNetworkIds = useRef({ latestRequest: 0, responseInState: 0 }); const [ autocompleteResponse, setAutocompleteResponse ] = useState(); - const latestAutocompleteResponseRef = useRef>(Promise.resolve()); + const responseToLatestRequestRef = useRef>(Promise.resolve()); async function executeAutocomplete () { const requestId = ++autocompleteNetworkIds.current.latestRequest; - latestAutocompleteResponseRef.current = new Promise(async (resolve) => { + responseToLatestRequestRef.current = new Promise(async (resolve) => { const response = isVertical ? await answersActions.executeVerticalAutocomplete() : await answersActions.executeUniversalAutocomplete(); + if (requestId === autocompleteNetworkIds.current.latestRequest) { + resolve(); + } if (requestId >= autocompleteNetworkIds.current.responseInState) { setAutocompleteResponse(response); autocompleteNetworkIds.current.responseInState = requestId; - if (requestId === autocompleteNetworkIds.current.latestRequest) { - resolve(); - } } }); } - return [ autocompleteResponse, latestAutocompleteResponseRef, executeAutocomplete ] + return [ autocompleteResponse, responseToLatestRequestRef, executeAutocomplete ] }; \ No newline at end of file