当前位置:网站首页>promise学习笔记
promise学习笔记
2022-08-08 15:51:00 【刘家奕_】
(1)Promise的介绍
(1) 异步编程

(2)为什么要用Promise?
(1)指定回调函数的方式更加灵活
(2)支持链式调用,可以解决回调地狱问题
回调地狱-------->(十分复杂)
(2)Promise的初体验
实现以下效果
(1)普通写法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<div class="container">
<h2 class="page-header">Promise初体验</h2>
<button class="btn btn-primary" id="btn">点击抽奖</button>
</div>
<script>
//生成随机数
function rand(m, n) {
return Math.ceil(Math.random() * (n - m + 1)) + m - 1;
}
const btn = document.querySelector("#btn");
btn.addEventListener("click", function () {
setTimeout(() => {
let n = rand(1, 100);
if (n <= 30) alert("恭喜恭喜,奖品为10万 RMB 劳斯莱斯优惠券");
else alert("再接再厉");
}, 2000);
});
</script>
</body>
</html>
(2)Promise写法
<script>
//生成随机数
function rand(m, n) {
return Math.ceil(Math.random() * (n - m + 1)) + m - 1;
}
const btn = document.querySelector("#btn");
btn.addEventListener("click", function () {
const p = new Promise((resolve, reject) => {
setTimeout(() => {
let n = rand(1, 100);
if (n <= 30) resolve();
else reject();
}, 2000);
});
p.then(
() => {
alert("恭喜恭喜,奖品为10万 RMB 劳斯莱斯优惠券");
},
() => {
alert("再接再厉");
}
);
});
</script>
(1)Promise写法(改进)传了数字结果---->n
<script>
//生成随机数
function rand(m, n) {
return Math.ceil(Math.random() * (n - m + 1)) + m - 1;
}
const btn = document.querySelector("#btn");
btn.addEventListener("click", function () {
const p = new Promise((resolve, reject) => {
setTimeout(() => {
let n = rand(1, 100);
if (n <= 30) resolve(n);
else reject(n);
}, 2000);
});
p.then(
(n) => {
alert("恭喜恭喜,奖品为10万 RMB 劳斯莱斯优惠券,中奖数字为" + n);
},
(n) => {
alert("再接再厉,数字为" + n);
}
);
});
</script> <script>
//生成随机数
function rand(m, n) {
return Math.ceil(Math.random() * (n - m + 1)) + m - 1;
}
const btn = document.querySelector("#btn");
btn.addEventListener("click", function () {
const p = new Promise((resolve, reject) => {
setTimeout(() => {
let n = rand(1, 100);
if (n <= 30) resolve(n);
else reject(n);
}, 2000);
});
p.then(
(n) => {
alert("恭喜恭喜,奖品为10万 RMB 劳斯莱斯优惠券,中奖数字为" + n);
},
(n) => {
alert("再接再厉,数字为" + n);
}
);
});
</script>
(3)Promise实践练习
(1)fs读取文件(普通写法)
(2)Promise写法
边栏推荐
猜你喜欢
解决Redis、MySQL缓存双写不一致问题
保险,一生必备
Notes on the development of kindergarten enrollment registration system based on WeChat applet
Superset 1.2.0 安装
[Online interviewer] How to achieve deduplication and idempotency
First online!Messaging middleware fairy notes, covering the essence of Alibaba's ten years of technology
(1)通过FlinkSQL将数据写入mysql demo
[Unity Starter Plan] Making RubyAdventure02 - Handling Tile Maps & Collision
Elegantly detect and update web applications in real time
有了这个开源工具后,我五点就下班了!
随机推荐
线程本地存储 ThreadLocal
【有奖征文 第13期】至简致远,“云”响世界,大胆秀出你的华为云技术主张,高额激励等你拿
web automation headless mode
bzoj3262 陌上花开
All volunteers V853 chip Tina RTSP environment set up
掌握财富密码,运维需要了解这些技术
A16z:为什么 NFT 创作者要选择 cc0?
一文搞懂│XSS攻击、SQL注入、CSRF攻击、DDOS攻击、DNS劫持
Mysql的分布式事务原理理解
vs2只运行项目中的一个文件
“科林明伦杯”哈尔滨理工大学暑假训练赛 B吃雪糕 (异或思维题)(补题)
JS-BOM-Name Converter - Input Name Position Reversed
C语言学习概览(六)
Redis RDB分析系统
快速排序(C语言版)
分布式架构服务调用
我分析30w条数据后发现,西安新房公摊最低的竟是这里?
领域驱动设计系列贫血模型和充血模型
Mysql数据库入门学习笔记
5G NR RRC连接控制