Quantcast
Channel: How do I return the response from an asynchronous call? - Stack Overflow
Viewing all articles
Browse latest Browse all 46

Answer by Vinoth Rajendran for How do I return the response from an asynchronous call?

$
0
0

You can use this custom library (written using Promise) to make a remote call.

function $http(apiConfig) {    return new Promise(function (resolve, reject) {        var client = new XMLHttpRequest();        client.open(apiConfig.method, apiConfig.url);        client.send();        client.onload = function () {            if (this.status >= 200 && this.status < 300) {                // Performs the function "resolve" when this.status is equal to 2xx.                // Your logic here.                resolve(this.response);            }            else {                // Performs the function "reject" when this.status is different than 2xx.                reject(this.statusText);            }        };        client.onerror = function () {            reject(this.statusText);        };    });}

Simple usage example:

$http({    method: 'get',    url: 'google.com'}).then(function(response) {    console.log(response);}, function(error) {    console.log(error)});

Viewing all articles
Browse latest Browse all 46

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>