'use strict';
// A-> $http function is implemented in order to follow the standard Adapter pattern
function $http(url){
// A small example of object
var core = {
// Method that performs the ajax request
ajax : function (method, url, args) {
// Creating a promise
var promise = new Promise( function (resolve, reject) {
// Instantiates the XMLHttpRequest
var client = new XMLHttpRequest();
var uri = url;
if (args && (method === 'POST' || method === 'PUT')) {
uri += '?';
var argcount = 0;
for (var key in args) {
if (args.hasOwnProperty(key)) {
if (argcount++) {
uri += '&';
}
uri += encodeURIComponent(key) + '=' + encodeURIComponent(args[key]);
}
}
}
client.open(method, uri);
client.send();
client.onload = function () {
if (this.status >= 200 && this.status < 300) {
// Performs the function "resolve" when this.status is equal to 2xx
resolve(this.response);
} else {
// Performs the function "reject" when this.status is different than 2xx
reject(this.statusText);
}
};
client.onerror = function () {
reject(this.statusText);
};
});
// Return the promise
return promise;
}
};
// Adapter pattern
return {
'get' : function(args) {
return core.ajax('GET', url, args);
},
'post' : function(args) {
return core.ajax('POST', url, args);
},
'put' : function(args) {
return core.ajax('PUT', url, args);
},
'delete' : function(args) {
return core.ajax('DELETE', url, args);
}
};
};
// End A
//B-> Here you define its functions and its payload
var mdnAPI = 'https://d...content-available-to-author-only...a.org/en-US/search.json';
var payload = {
'topic' : 'js',
'q' : 'Promise'
};
var callback = {
success : function(data){
console.log(1, 'success', JSON.parse(data));
},
error : function(data){
console.log(2, 'error', JSON.parse(data));
}
};
// End B
// Executes the method call
$http(mdnAPI)
.get(payload)
.then(callback.success)
.catch(callback.error);
// Executes the method call but an alternative way (1) to handle Promise Reject case
$http(mdnAPI)
.get(payload)
.then(callback.success, callback.error);
// Executes the method call but an alternative way (2) to handle Promise Reject case
$http(mdnAPI)
.get(payload)
.then(callback.success)
.then(undefined, callback.error);
J3VzZSBzdHJpY3QnOwoKLy8gQS0+ICRodHRwIGZ1bmN0aW9uIGlzIGltcGxlbWVudGVkIGluIG9yZGVyIHRvIGZvbGxvdyB0aGUgc3RhbmRhcmQgQWRhcHRlciBwYXR0ZXJuCmZ1bmN0aW9uICRodHRwKHVybCl7CiAKICAvLyBBIHNtYWxsIGV4YW1wbGUgb2Ygb2JqZWN0CiAgdmFyIGNvcmUgPSB7CgogICAgLy8gTWV0aG9kIHRoYXQgcGVyZm9ybXMgdGhlIGFqYXggcmVxdWVzdAogICAgYWpheCA6IGZ1bmN0aW9uIChtZXRob2QsIHVybCwgYXJncykgewoKICAgICAgLy8gQ3JlYXRpbmcgYSBwcm9taXNlCiAgICAgIHZhciBwcm9taXNlID0gbmV3IFByb21pc2UoIGZ1bmN0aW9uIChyZXNvbHZlLCByZWplY3QpIHsKCiAgICAgICAgLy8gSW5zdGFudGlhdGVzIHRoZSBYTUxIdHRwUmVxdWVzdAogICAgICAgIHZhciBjbGllbnQgPSBuZXcgWE1MSHR0cFJlcXVlc3QoKTsKICAgICAgICB2YXIgdXJpID0gdXJsOwoKICAgICAgICBpZiAoYXJncyAmJiAobWV0aG9kID09PSAnUE9TVCcgfHwgbWV0aG9kID09PSAnUFVUJykpIHsKICAgICAgICAgIHVyaSArPSAnPyc7CiAgICAgICAgICB2YXIgYXJnY291bnQgPSAwOwogICAgICAgICAgZm9yICh2YXIga2V5IGluIGFyZ3MpIHsKICAgICAgICAgICAgaWYgKGFyZ3MuaGFzT3duUHJvcGVydHkoa2V5KSkgewogICAgICAgICAgICAgIGlmIChhcmdjb3VudCsrKSB7CiAgICAgICAgICAgICAgICB1cmkgKz0gJyYnOwogICAgICAgICAgICAgIH0KICAgICAgICAgICAgICB1cmkgKz0gZW5jb2RlVVJJQ29tcG9uZW50KGtleSkgKyAnPScgKyBlbmNvZGVVUklDb21wb25lbnQoYXJnc1trZXldKTsKICAgICAgICAgICAgfQogICAgICAgICAgfQogICAgICAgIH0KCiAgICAgICAgY2xpZW50Lm9wZW4obWV0aG9kLCB1cmkpOwogICAgICAgIGNsaWVudC5zZW5kKCk7CgogICAgICAgIGNsaWVudC5vbmxvYWQgPSBmdW5jdGlvbiAoKSB7CiAgICAgICAgICBpZiAodGhpcy5zdGF0dXM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