/ Published in: JavaScript
wrapping XMLHttpRequest in Promise
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
function callget(url){ return new Promise(function(resolve,reject){ var oReq = new XMLHttpRequest(); oReq.open("GET", url); oReq.onload = function(){ if(oReq.status ==200){ resolve(oReq.response); } else{ reject(oReq.statusText); } }; oReq.error = function(){ reject('Network error'); }; oReq.send(); }); } var jq = 'https://code.jquery.com/jquery-33.2.1.min.js'; callget(jq).then(finish).catch(finish); function finish(x){ console.log(x); }