当前位置:网站首页>Promise实例如何解决地狱回调
Promise实例如何解决地狱回调
2022-07-28 08:50:00 【前端程序】
系列文章目录
提示:今天下午公司同事有问到有条件发送网络请求
情景如下:发起第一个请求,拿到第一个请求后;才能继续发送第二个请求;第二个请求发送完毕后,用第二个参数第三个请求。
上面的这个情景就是地狱回调的问题,这时候我们可以使用封装Promise方法去解决我们一直在 .then() 中写请求的方式,同时也让我们的代码更加的具有可读性。
提示:写完文章后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
一、地狱回调(代码案例):
//fetch就是我们的网络请求(这里是模拟与举例子)
fetch("http://127.0.0.0:3000/").then((res) => {
console.log("yi !");
fetch("http://127.0.0.0:3000/diyi").then((res) => {
console.log("er !!");
fetch("http://127.0.0.0:3000/dier").then((res) => {
console.log("san !!!");
});
});
});
这里打印的结果是:
第一次:yi !
第二次:er !!
第三次:san !!!
二、什么是Promise?
Promise:是一个构造函数,用来传递异步操作消息,链式调用,避免层层嵌套的回调函数。
- promise接收两个函数参数,resolve和reject,分别表示异步操作执行成功后的回调和失败的回调
- Promise有三种状态:pending进行中、resolve已完成、rejected已失败, 这些状态只能由pending ->
resolved, pending -> rejected,一旦promise实例发生改变,就不能在变了,任何时候都能得到这个结果
三、使用Promise解决地狱回调:
1.写一个返回Promise实例的方法
代码分析图如下

代码如下(示例):
//requestMethods 是一个函数(方法),可以传递a,b,c三个参数
function requestMethods(a, b, c) {
//这一步骤是将 new Promise 的实例返回给 requestMethods函数
return new Promise((resolve, reject) => {
//当b=2(b是第二次请求的结果),c=3 的时候执行
if (b == 2 && c == 3) {
setTimeout(() => {
resolve(10);
}, 2000);
return;
}
//当a=1(a是一次请求得到结果),b=2 的时候执行
if (a == 1 && b == 2) {
setTimeout(() => {
resolve(b);
}, 2000);
return;
}
//当a=1 的时候执行
if (a == 1) {
resolve(a);
return;
}
});
}
2.调用 requestMethods()查看结果
代码分析图如下

代码如下(示例):
//这里是第一次网络请求(模拟)
requestMethods((a = 1))
//第一次的请求结果 then 回调函数
.then((res) => {
console.log(res, "a");
//这里是第二次网络请求(模拟)
return requestMethods((a = res), (b = 2));
})
//第二次的请求结果
.then((res) => {
console.log(res, "b");
//这里是第三次网络请求(模拟)
return requestMethods((a = 1), (b = res), (c = 3));
})
//第三次的请求结果
.then((res) => {
console.log(res, "c");
});
总结
提示:这里对文章进行总结:总体上Api是非常容易理解的,我给同事解释了一遍就懂了,希望文章对大家是有用的,同时我写文章也能提升一下我自己的掌握程度,希望大家都可以非常的!!!!!!! 加油!!!!!!
边栏推荐
- 这两套代码有什么区别呢?
- mysql5.7.38容器里启动keepalived
- c# 有符号和无符号字节变量
- DAPP safety summary and typical safety incident analysis
- Hou Jie STL standard library and generic programming
- [C language] detailed explanation sequence table (seqlist)
- Setting of parameter configuration tool for wireless vibrating wire collector
- opencv4.60版本安装和配置
- Starfish Os打造的元宇宙生态,跟MetaBell的合作只是开始
- [package deployment]
猜你喜欢
![[592. Fraction addition and subtraction]](/img/3a/1a76a8acd60a6d45eebed612fd3971.png)
[592. Fraction addition and subtraction]

公众号简介

2022年起重机司机(限桥式起重机)考试题库及模拟考试

Alibaba cloud server setup and pagoda panel connection

2022年危险化学品经营单位安全管理人员上岗证题目及答案

CSV file storage

个人博客小程序
![[solution] error in [eslint] eslint is not a constructor](/img/58/2ce1243d0085462af3ba6d3da0817d.png)
[solution] error in [eslint] eslint is not a constructor

2022 safety officer-c certificate special operation certificate examination question bank and answers

技术分享| 快对讲综合调度系统
随机推荐
Linux initializes MySQL with fatal error: could not find my-default.cnf
2022年危险化学品经营单位安全管理人员上岗证题目及答案
从开发转测试:我从零开始,一干就是6年的自动化测试历程
Promise学习笔记
From development to testing: I started from scratch and worked for six years of automated testing
What is the difference between these two sets of code?
CakePHP 4.4.3 发布,PHP 快速开发框架
Title and answer of work permit for safety management personnel of hazardous chemical business units in 2022
FPGA开发学习开源网站汇总
Activiti startup error: cannot create poolableconnectionfactory (could not create connection to database server
A perfect cross compilation environment records the shell scripts generated by PETA
Modify virtual machine IP address
Recommend an artifact to get rid of the entanglement of variable names and a method to modify file names in batches
376. Swing sequence [greedy, dynamic planning -----]
The chess robot pinched the finger of a 7-year-old boy, and the pot of a software testing engineer? I smiled.
51 single chip microcomputer storage: EEPROM (I2C)
Magic Bracelet-【群论】【Burnside引理】【矩阵快速幂】
阿里云服务器搭建和宝塔面板连接
C language array pointer and pointer array discrimination, analysis of memory leakage
LeetCode_406_根据身高重建队列