Skip to content

Commit b4e6fec

Browse files
author
linzhen34
committed
ajaxPromise
1 parent 3e1af08 commit b4e6fec

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

ajaxPromise.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//基于promise封装ajax
2+
// 返回一个新的Promise实例
3+
4+
5+
// 创建HMLHttpRequest异步对象
6+
7+
8+
// 调用open方法,打开url,与服务器建立链接(发送前的一些处理)
9+
10+
// 监听Ajax状态信息
11+
12+
// 如果xhr.readyState == 4(表示服务器响应完成,可以获取使用服务器的响应了)
13+
14+
// xhr.status == 200,返回resolve状态
15+
// xhr.status == 404,返回reject状态
16+
17+
// xhr.readyState !== 4,把请求主体的信息基于send发送给服务器
18+
19+
function ajax(url,method,data){
20+
return new Promise((resolve,reject)=>{
21+
let xhr = new XMLHttpRequest
22+
xhr.open(url,method)
23+
xhr.send(data)
24+
xhr.onreadystatechange=()=>{
25+
if(xhr.readyState&&xhr.status==200){
26+
resolve(xhr.responseText)
27+
}else{
28+
reject(xhr.status)
29+
}
30+
}
31+
})
32+
}

newCreat.js

+3
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,6 @@ function newFlag(Fnc){
1515
}
1616
return obj
1717
}
18+
19+
let a = newFlag(Array)
20+
console.log(Array.isArray(a));

0 commit comments

Comments
 (0)