■ES6中一个非常重要和好用的特性就是Promise
- Promise是异步编程的一种解决方案
- 使用Promise后异步操作以同步操作的流程表达出来,避免了层层嵌套的回调函数
■那什么时候我们会来处理异步事件呢?
- 一种很常见的场景应该就是网络请求了
- 我们封装一个网络清求的函数,因为不能立即拿到结果,所以不能像简单的3+4=7-样将结果返回
- 所以往往我们会传入另外一个函数,在数据请求成功时,将数据通过传入的函数回调出去
- 如果只是一个简单的网络请求,那么这种方案不会给我们带来很大的麻烦
- 但是,当网络请求非常复杂时,就会出现回调地狱(看下面的场景)。
■我们来考虑下面的场景(有夸张的成分)
- 我们需要通过一个url1从服务器加载一个数据datal , data1中包含了下一个请求的url2
- 我们需要通过data1取出url2。从服务器加载数据data2,data2中包含了下一个请求的url3
- 我们需要通过data2取出url3。从服务器加载数据data3,data3中包含了下一个请求的url4
- 发送网络请求url4,获取最终的数据data4
■上面的代码有什么问题吗?
- 正常情况下不会有生么问题,可以正常运行并且获得我们想要的结果
- 但是这样的代码不适合维护,我们希望使用一种更加优雅的方法来进行这种异步在操作
- 这种方法就是使用Promise
Promise的基本使用
我们用一个定时器来模拟异步事件
- 在代码中data是从网络上一秒后请求到的数据
- console.log(data)是处理方式
- 先来看看过去的写法
setTimeout(() => { let data = "Hello World"; console.log(data); },1000)
换成Promise写法
new Promise((resolve,reject) => {
setTimeout(() => {
//请求到的数据
let data = "Hello World";
resolve(data);
reject();
},1000)
}).then(data => {
//请求到数据后要进行的操作
console.log(data);
}).catch(() => {
console.log("error");
})
■Promise是一个对象,需要传入一个回调函数
- 回调函数中有两个参数resolve,reject,它们都是回调函数
- 当请求成功时调用resolve()
- 当请求失败时调用reject()
■调用resolve后会执行then()方法
- then()中需要传入一个回调函数
- 在回调函数中写请求成功后要执行的代码
- then()会返回一个Promise对象
■调用reject后会执行catch()方法
- catch()中需要传入一个回调函数
- 在回调函数中写请求失败后要执行的代码
- catch()会返回一个Promise对象
■可见Promise最大的好处是在异步执行的流程中,把执行代码和处理结果的代码清晰地分离了
我们在来看看更复杂的情况
- 有一个定时器a,一秒后输出data1
- 在定时器a中,有一个定时器b
- 定时器b,一秒后输出data2
- 在定时器b中,有一个定时器c
- 定时器c,一秒后输出data3
//定时器a
setTimeout(() => {
console.log("data1");
//定时器b
setTimeout(() =>{
console.log("data2");
//定时器c
setTimeout(() =>{
console.log("data3")
},1000)
},1000)
}1000)
(看到这个,有没有想到上面说的回调地狱?)
来看看使用Promise的代码
new Promise((resolve,reject) => {
//定时器a
setTimeout(() => {
resolve();
},1000)
}).then(() => {
console.log("data1");
return new Promise((resolve,reject) => {
//定时器b
setTimeout(() => {
resolve();
},1000)
}).then(() => {
console.log("data2");
return new Promise((resolve,reject) => {
//定时器c
setTimeout(() => {
resolve();
},1000)
}).then(() => {
console.log("data3");
}).catch(() => {
console.log("error");
})
}).catch(() => {
console.log("error");
})
}).catch(() => {
console.log("error");
})
- 在使用Promise时,如果在请求a中还有一个b请求,那我们可以在a的then中返回一个Promise对象,将b请求放在这里面执行。
- 看到这个代码是不是突然觉得我还是用以前的方法好!(套娃警告)
不急,我们来看看清楚一点的写法
//第三个请求
function req3(){
let p3 = new Promise((resolve,reject) => {
//定时器3
setTimeout(() => {
resolve()
},1000)
}).then(() => {
console.log("data3")
}).catch(() => {
console.log("error");
})
return p3;
}
//第二个请求
function req2(){
let p2 = new Promise((resolve,reject) => {
//定时器2
setTimeout(() => {
resolve()
},1000)
}).then(() => {
console.log("data2")
//执行第三个请求
req3();
}).catch(() => {
console.log("error");
})
return p2;
}
//第一个请求
new Promise((resolve,reject) => {
//定时器1
setTimeout(() => {
resolve()
},1000)
}).then(() => {
console.log("data1");
//执行第二个请求
req2();
}).catch(() => {
console.log("error");
})
是不是比上一个清楚多了,虽然看起来比以前的写法多了好多代码,但却完美的解决了地狱回调的问题,并且把每个请求的执行代码和处理结果的代码清晰地分离了。
Promise的三种状态
■当我们开发中有异步操作时,就可以给异步操作包装一个Promise
- 异步操作后会有三种状态
■Promise的三种状态
- pending:等待状态,比如正在进行网络请求,或者定时器没有到时间
- fulfill:满足状态,当我们主动回调resolve时,就处于该状态,并且会回调then()
- reject :拒绝状态,当我们主动回调reject 时,就处于该状态,并且会回调catch()
buy arimidex for sale order anastrozole 1mg online cheap anastrozole 1 mg uk
arimidex 1 mg sale buy anastrozole generic order anastrozole 1mg
biaxin 250mg sale meclizine cost meclizine over the counter
naproxen 500mg ca naprosyn pills lansoprazole sale
naproxen brand lansoprazole 30mg usa cheap prevacid 30mg
cheap biaxin buy clarithromycin sale buy generic antivert 25 mg
order spiriva 9mcg without prescription order tiotropium bromide 9 mcg without prescription cheap hytrin 1mg
proventil 100mcg without prescription order protonix 20mg online cheap ciprofloxacin 500mg cost
purchase albuterol for sale purchase proventil online order ciprofloxacin 1000mg without prescription
buy tiotropium bromide without prescription order minocycline 100mg without prescription hytrin sale
buy pioglitazone for sale sildenafil 100mg tablet viagra sildenafil 25mg
singulair online sildenafil 50mg us sildenafil 100mg pill
order montelukast 10mg singulair over the counter order sildenafil 50mg pills
actos 30mg pill brand sildenafil 100mg pfizer viagra
discount cialis Price cialis free casino slot games
cheap cialis 20mg Cialis mail order purchase tadalafil online cheap
cialis 20mg for sale sexual dysfunction brand cialis 20mg
cialis 5mg us free blackjack games free blackjack games
online casino real money paypal no deposit free spins blackjack online free
ivermectin dose for covid generic stromectol for humans oral avlosulfon 100mg
ivermectin 12 mg over the counter order symmetrel 100mg generic dapsone canada
bonus poker online free spins casino poker online for real money
nifedipine over the counter aceon 8mg brand purchase fexofenadine without prescription
wind creek casino online games online casino real money no deposit play roulette for free
online blackjack real money online blackjack with real money write me a paper
buy ramipril 5mg generic order altace 5mg pill buy arcoxia
buy altace 5mg without prescription order amaryl online cheap order arcoxia 60mg without prescription
poker sites best online canadian pharmacy pay for essays
essay writing assistance sulfasalazine 500mg price azulfidine canada
doxycycline buy online order doxycycline pills cleocin 300mg usa
order generic doxycycline buy cleocin generic clindamycin pill
writing assignments leflunomide 10mg tablet order azulfidine pills
order generic olmesartan order depakote online cheap order depakote 500mg online
order mesalamine online cheap mesalamine uk irbesartan cost
order generic benicar 20mg buy calan without prescription cost depakote 250mg
clobetasol cheap buspirone usa cheap amiodarone
purchase temovate for sale cordarone 100mg usa order cordarone for sale
diamox without prescription cost acetazolamide 250mg purchase azathioprine generic
acetazolamide cost order azathioprine 25mg generic order generic azathioprine 50mg
buy digoxin 250 mg generic molnunat order molnupiravir pill
buy digoxin 250mg pills buy micardis generic buy molnupiravir pill
buy coreg 6.25mg online cheap order elavil 50mg without prescription amitriptyline 50mg cost
amoxil 500mg cheap stromectol oral ivermectin 12 mg tablets for humans
brand carvedilol 25mg purchase elavil online cheap elavil pills
amoxicillin 1000mg over the counter buy amoxil 250mg pills ivermectin 6 mg pills for humans
alendronate 35mg cost nitrofurantoin medication motrin medication
purchase priligy without prescription buy priligy 60mg pill buy generic motilium 10mg
fosamax 70mg cost buy alendronate 70mg without prescription order ibuprofen 600mg online cheap
priligy 90mg cheap motilium uk cost domperidone
oral nortriptyline paxil over the counter order generic paroxetine 20mg
order indomethacin 75mg without prescription order cenforce sale cenforce 50mg pill
buy pamelor without prescription buy nortriptyline paxil for sale online
indomethacin us order flomax for sale order cenforce 100mg online
famotidine order remeron 30mg ca order mirtazapine online cheap
order doxycycline 200mg for sale oral doxycycline 200mg methylprednisolone 16mg tablets
famotidine sale buy pepcid 20mg without prescription buy mirtazapine 30mg generic
buy doxycycline 200mg generic brand medrol medrol without prescription
buy requip 2mg generic purchase ropinirole online purchase labetalol generic
generic ropinirole 2mg requip pill buy labetalol 100 mg without prescription
tadalafil for sale online buy amoxicillin 500mg online trimox 500mg tablet
fenofibrate over the counter fenofibrate 160mg usa buy sildenafil generic
tadalafil 10mg over the counter order generic trimox 500mg buy trimox sale
order fenofibrate without prescription fenofibrate 200mg pills sildenafil pill
nexium 40mg pill lasix 40mg for sale buy lasix without prescription
tadalafil oral Cialis mail order usa sildenafil citrate
buy esomeprazole sale buy clarithromycin 500mg pills lasix order
order tadalafil 40mg without prescription viagra online order sildenafil 100mg oral
buy minocycline 100mg generic purchase minocin without prescription order terazosin
buy cialis 40mg pill can i buy ed pills over the counter ed pills
buy minocycline 50mg capsules cheap gabapentin pill cheap terazosin
cialis online buy ed medications online ed pills cheap
glycomet uk generic nolvadex 20mg tamoxifen cost
buy modafinil generic buy modafinil 200mg online promethazine canada
brand metformin buy generic tamoxifen 20mg order tamoxifen 10mg
clomiphene uk clomid 50mg ca order prednisolone 10mg online
prednisone 10mg cost order deltasone 40mg order amoxicillin without prescription
order clomiphene 50mg online cheap prednisolone generic purchase prednisolone sale
prednisone usa deltasone pills purchase amoxicillin pill
buy absorica online buy generic accutane 20mg order generic ampicillin
buy fildena 100mg without prescription order fildena pills purchase proscar pills
absorica for sale online buy deltasone 5mg generic ampicillin 250mg canada
buy sildenafil 100mg generic order finasteride 5mg pill propecia 5mg pill
ivermectin 12mg for humans stromectol 12mg canada deltasone usa
order ondansetron 8mg online cheap cheap zofran 8mg bactrim over the counter
ivermectina 6mg cost stromectol 12mg buy prednisone 20mg generic
buy zofran 8mg amoxil 500mg pill buy bactrim 480mg without prescription
accutane 40mg usa accutane without prescription oral azithromycin 500mg
buy generic accutane order isotretinoin 10mg without prescription buy azithromycin 250mg generic
albuterol order online albuterol over the counter order amoxiclav for sale
modafinil us metoprolol 100mg canada buy metoprolol online cheap
brand prednisolone 10mg furosemide 100mg generic generic lasix 40mg
provigil medication buy metoprolol 50mg without prescription buy metoprolol
prednisolone 5mg cheap lasix 100mg usa furosemide 40mg cost
avodart order order avodart for sale order orlistat 60mg pill
buy monodox generic buy zovirax 400mg online acyclovir 800mg over the counter
dutasteride online order order orlistat generic xenical online order