JavaScript makes AJAX requests asynchronously, so you can't expect the code to wait for a return value. You have to use a callback function to process the data when it is returned.
In the past you would use a library to make HTTP requests, like jQuery, the AngularJS $http service, or the Axios library (used in older ReactJS applications).
Now JavaScript supports a new API called Fetch.
These libraries, and Fetch, return an object known as a Promise object. The Promise wraps the actual request function in a handler that enables you configure callback functions that are called upon success or failure of the request.