当前位置:网站首页>How promise instance solves hell callback
How promise instance solves hell callback
2022-07-28 09:29:00 【Front end program】
List of articles
Tips : This afternoon, colleagues in the company asked about sending network requests conditionally
The scene is as follows : Initiate the first request , After getting the first request ; To continue sending the second request ; After the second request is sent , Use the second parameter and the third request .
The above scenario is the problem of hell callback , At this time, we can use encapsulation Promise Methods to solve what we have been doing .then() Write the request in , It also makes our code more readable .
Tips : After writing the article , Directories can be generated automatically , How to generate it, please refer to the help document on the right
List of articles
One 、 Hell callback ( Code case ):
//fetch Is our network request ( Here are simulations and examples )
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 !!!");
});
});
});
The result printed here is :
for the first time :yi !
The second time :er !!
third time :san !!!
Two 、 What is? Promise?
Promise: It's a constructor , Used to deliver asynchronous operation messages , call chaining , Avoid nested callback functions .
- promise Receive two function parameters ,resolve and reject, It represents the callback after successful execution of asynchronous operation and the callback after failure
- Promise There are three states :pending Have in hand 、resolve Completed 、rejected Failed , These states can only be determined by pending ->
resolved, pending -> rejected, once promise The instance has changed , Can't change , You can get that at any time
3、 ... and 、 Use Promise Solve hell callback :
1. Write a return Promise Example method
The code analysis diagram is as follows

The code is as follows ( Example ):
//requestMethods It's a function ( Method ), It can deliver a,b,c Three parameters
function requestMethods(a, b, c) {
// This step is to new Promise Return to requestMethods function
return new Promise((resolve, reject) => {
// When b=2(b It is the result of the second request ),c=3 When it comes to execution
if (b == 2 && c == 3) {
setTimeout(() => {
resolve(10);
}, 2000);
return;
}
// When a=1(a It's a request to get the result ),b=2 When it comes to execution
if (a == 1 && b == 2) {
setTimeout(() => {
resolve(b);
}, 2000);
return;
}
// When a=1 When it comes to execution
if (a == 1) {
resolve(a);
return;
}
});
}
2. call requestMethods() View results
The code analysis diagram is as follows

The code is as follows ( Example ):
// This is the first network request ( simulation )
requestMethods((a = 1))
// The first request result then Callback function
.then((res) => {
console.log(res, "a");
// This is the second network request ( simulation )
return requestMethods((a = res), (b = 2));
})
// The result of the second request
.then((res) => {
console.log(res, "b");
// This is the third network request ( simulation )
return requestMethods((a = 1), (b = res), (c = 3));
})
// The third request result
.then((res) => {
console.log(res, "c");
});
summary
Tips : Here is a summary of the article : Overall Api It's very easy to understand , I explained it to my colleagues and understood , I hope this article is useful to you , At the same time, writing articles can also improve my own mastery , I hope everyone can be very !!!!!!! come on. !!!!!!
边栏推荐
- 【SwinTransformer源码阅读二】Window Attention和Shifted Window Attention部分
- IT行业数据与应用关系的变迁
- What is the difference between these two sets of code?
- 51 single chip microcomputer storage: EEPROM (I2C)
- [592. Fraction addition and subtraction]
- golang升级到1.18.4版本 遇到的问题
- 【高数】高数平面立体几何
- 5 operators, expressions, and statements
- Starfish Os打造的元宇宙生态,跟MetaBell的合作只是开始
- 蓝牙技术|2025年北京充电桩总规模达70万个,聊聊蓝牙与充电桩的不解之缘
猜你喜欢

Final keyword and enumeration type

数据库核心体系

股指期货开户的条件和流程

An entry artifact tensorflowplayground

Promise learning notes

2022 high voltage electrician examination simulated 100 questions and simulated examination

Title and answer of work permit for safety management personnel of hazardous chemical business units in 2022

Network engineering -- ranking of Chinese universities in Soft Science

JDBC连接数据库

2022安全员-C证特种作业证考试题库及答案
随机推荐
[package deployment]
7 C control statements: branches and jumps
2022 high voltage electrician examination simulated 100 questions and simulated examination
蓝牙技术|2025年北京充电桩总规模达70万个,聊聊蓝牙与充电桩的不解之缘
ES6 let and Const
[Download] several tools for brute force cracking and dictionary generation are recommended
oracle 创建用户且只有查询权限
【vscode】vscode使用
正负数值的正则表达式
【一花一世界-郑一教授-繁简之道】可解释神经网络
An entry artifact tensorflowplayground
如何在多线程环境下使用 GBase C API ?
OpenShift 4 之AMQ Streams(1) - 多个Consumer从Partition接收数据
2022年安全员-B证考试模拟100题及答案
Detailed introduction of v-bind instruction
v-bind指令的详细介绍
OpenShift 4 - 使用 VerticalPodAutoscaler 优化应用资源 Request 和 Limit
Oracle creates users with query permission only
【英语考研词汇训练营】Day 15 —— analyst,general,avoid,surveillance,compared
leetcode 452. Minimum Number of Arrows to Burst Balloons 用最少数量的箭引爆气球(中等)