fork(1) download
  1. 'use strict';
  2.  
  3. // A-> $http function is implemented in order to follow the standard Adapter pattern
  4. function $http(url){
  5.  
  6. // A small example of object
  7. var core = {
  8.  
  9. // Method that performs the ajax request
  10. ajax : function (method, url, args) {
  11.  
  12. // Creating a promise
  13. var promise = new Promise( function (resolve, reject) {
  14.  
  15. // Instantiates the XMLHttpRequest
  16. var client = new XMLHttpRequest();
  17. var uri = url;
  18.  
  19. if (args && (method === 'POST' || method === 'PUT')) {
  20. uri += '?';
  21. var argcount = 0;
  22. for (var key in args) {
  23. if (args.hasOwnProperty(key)) {
  24. if (argcount++) {
  25. uri += '&';
  26. }
  27. uri += encodeURIComponent(key) + '=' + encodeURIComponent(args[key]);
  28. }
  29. }
  30. }
  31.  
  32. client.open(method, uri);
  33. client.send();
  34.  
  35. client.onload = function () {
  36. if (this.status >= 200 && this.status < 300) {
  37. // Performs the function "resolve" when this.status is equal to 2xx
  38. resolve(this.response);
  39. } else {
  40. // Performs the function "reject" when this.status is different than 2xx
  41. reject(this.statusText);
  42. }
  43. };
  44. client.onerror = function () {
  45. reject(this.statusText);
  46. };
  47. });
  48.  
  49. // Return the promise
  50. return promise;
  51. }
  52. };
  53.  
  54. // Adapter pattern
  55. return {
  56. 'get' : function(args) {
  57. return core.ajax('GET', url, args);
  58. },
  59. 'post' : function(args) {
  60. return core.ajax('POST', url, args);
  61. },
  62. 'put' : function(args) {
  63. return core.ajax('PUT', url, args);
  64. },
  65. 'delete' : function(args) {
  66. return core.ajax('DELETE', url, args);
  67. }
  68. };
  69. };
  70. // End A
  71.  
  72. //B-> Here you define its functions and its payload
  73.  
  74. var mdnAPI = 'https://d...content-available-to-author-only...a.org/en-US/search.json';
  75. var payload = {
  76. 'topic' : 'js',
  77. 'q' : 'Promise'
  78. };
  79.  
  80. var callback = {
  81. success : function(data){
  82. console.log(1, 'success', JSON.parse(data));
  83. },
  84. error : function(data){
  85. console.log(2, 'error', JSON.parse(data));
  86. }
  87. };
  88. // End B
  89.  
  90. // Executes the method call
  91. $http(mdnAPI)
  92. .get(payload)
  93. .then(callback.success)
  94. .catch(callback.error);
  95.  
  96. // Executes the method call but an alternative way (1) to handle Promise Reject case
  97. $http(mdnAPI)
  98. .get(payload)
  99. .then(callback.success, callback.error);
  100.  
  101. // Executes the method call but an alternative way (2) to handle Promise Reject case
  102. $http(mdnAPI)
  103. .get(payload)
  104. .then(callback.success)
  105. .then(undefined, callback.error);
Runtime error #stdin #stdout #stderr 0.01s 30384KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
prog.js:13:0 ReferenceError: Promise is not defined