const Ajaxs = (method, url, data) => {
let promise = new Promise((resolve, reject) => {
let ajax = new XMLHttpRequest()
if (method.toLocaleLowerCase() === 'get') {
if (data) {
ajax.open(method, url + '?' + data, true)
} else {
ajax.open(method, url, true)
}
ajax.send()
} else {
ajax.open(method, url, true)
ajax.setRequestHeader('Content-type', 'application/x-www-form-urlencoded')
if (data) {
ajax.send(data)
} else {
ajax.send()
}
}
ajax.onreadystatechange = () => {
if (ajax.readyState !== 4) {
return
}
if (ajax.status === 200) {
resolve(ajax.responseText)
} else {
reject(new Error(ajax.statusText))
}
}
});
return promise
}
<script src="./promise.js"></script>
<script>
let result = Ajaxs('get', './3.json', '666')
console.log('result for 24 line', result);
let type1 = result.then((res) => {
console.log('res', res);
}, (err) => {
console.log('err', err);
})
console.log('type1', type1);
</script>