No nonsense, sticky Code
function ajax(method, url, data) { let request = new XMLHttpRequest(); return new Promise(function (resolve, reject) { request.onreadystatechange = function () { if (request.readyState === 4) { if (request.status === 200) { resolve(request.responseText); } else { reject(request.status); } } }; the request.open (Method, URL); request.send (Data); }); } the let P = Ajax ( 'the GET', 'config.json' ); p.then ( function (text) { // If successful AJAX , the content is obtained in response the console.log (text); }). the catch ( function (Status) { // If the AJAX fails to obtain a response code the console.log (Status); });
These are the realization of simulation ajax
let promise=new Promise((resolve, reject) => { console.log("1") reject("我错了"); resolve("123"); }); promise.then(function(data){ console.log("3") console.log(data) },function(err){ console.log("4") console.log(err) }) console.log("2")
These are used to test the execution order of promise