当前位置:网站首页>es6 Promise
es6 Promise
2022-07-28 17:44:00 【Cang jiuxiao Hai】
One 、Promise produce
Promise The implementation of the . Promise It's a constructor , I have all、reject、resolve These familiar methods , Prototype has then、catch And so on .
It would be new One
var p = new Promise(function(resolve, reject){ // Do some asynchronous operations setTimeout(function(){ console.log(' Execution completed '); resolve(' Any data '); }, 2000); });
function runAsync(){ var p = new Promise(function(resolve, reject){ // Do some asynchronous operations setTimeout(function(){ console.log(' Execution completed '); resolve(' Any data '); }, 2000); }); return p; } runAsync()
runAsync().then(function(data){ console.log(data); // You can use the data to do other operations later //...... });
function runAsync(callback){ setTimeout(function(){ console.log(' Execution completed '); callback(' Any data '); }, 2000); } runAsync(function(data){ console.log(data); });
Usage of chain operation
runAsync1() .then(function(data){ console.log(data); return runAsync2(); }) .then(function(data){ console.log(data); return runAsync3(); }) .then(function(data){ console.log(data); });

Guess runAsync1、runAsync2、runAsync3 How these three functions are defined ? you 're right , That's what it looks like
function runAsync1(){ var p = new Promise(function(resolve, reject){ // Do some asynchronous operations setTimeout(function(){ console.log(' Asynchronous task 1 Execution completed '); resolve(' Any data 1'); }, 1000); }); return p; } function runAsync2(){ var p = new Promise(function(resolve, reject){ // Do some asynchronous operations setTimeout(function(){ console.log(' Asynchronous task 2 Execution completed '); resolve(' Any data 2'); }, 2000); }); return p; } function runAsync3(){ var p = new Promise(function(resolve, reject){ // Do some asynchronous operations setTimeout(function(){ console.log(' Asynchronous task 3 Execution completed '); resolve(' Any data 3'); }, 2000); }); return p; }
runAsync1() .then(function(data){ console.log(data); return runAsync2(); }) .then(function(data){ console.log(data); return ' Direct return data '; // Data is directly returned here }) .then(function(data){ console.log(data); });
So that's what the output is like :

reject Usage of
function getNumber(){ var p = new Promise(function(resolve, reject){ // Do some asynchronous operations setTimeout(function(){ var num = Math.ceil(Math.random()*10); // Generate 1-10 The random number if(num<=5){ resolve(num); } else{ reject(' The number is too big '); } }, 2000); }); return p; } getNumber() .then( function(data){ console.log('resolved'); console.log(data); }, function(reason, data){ console.log('rejected'); console.log(reason); } );
catch Usage of
getNumber() .then(function(data){ console.log('resolved'); console.log(data); }) .catch(function(reason){ console.log('rejected'); console.log(reason); });
getNumber() .then(function(data){ console.log('resolved'); console.log(data); console.log(somedata); // Here somedata Undefined }) .catch(function(reason){ console.log('rejected'); console.log(reason); });

all Usage of
Promise
.all([runAsync1(), runAsync2(), runAsync3()])
.then(function(results){ console.log(results); });
race Usage of
Promise
.race([runAsync1(), runAsync2(), runAsync3()])
.then(function(results){ console.log(results); });
// Request a picture resource function requestImg(){ var p = new Promise(function(resolve, reject){ var img = new Image(); img.onload = function(){ resolve(img); } img.src = 'xxxxxx'; }); return p; } // The time delay function , Used to time requests function timeout(){ var p = new Promise(function(resolve, reject){ setTimeout(function(){ reject(' Picture request timeout '); }, 5000); }); return p; } Promise .race([requestImg(), timeout()]) .then(function(results){ console.log(results); }) .catch(function(reason){ console.log(reason); });
边栏推荐
- 漫谈测试平台—平台建设思路(上)
- Punctual atomic serial port protocol
- 禅道项目管理软件,敏捷开发团队不可或缺的工具
- 2021 National Undergraduate data statistics and Analysis Competition
- 软件测试前景不行了吗?进入寒冬期了吗?
- Ng repeat executes a method when iterating over the last element
- In depth sharing of Ali (ant financial) technical interview process, with preliminary preparation and learning direction
- 阿里云天池大赛赛题解析(深度学习篇)--阅读笔记1--赛题一
- Using OCR to reverse recognize text content in airtest
- Management of third-party technical services in product development
猜你喜欢
@RequestMapping详解

Hgu95av2. Online installation failed

An article takes you closer to the overview and principle of kubernetes

Ggplot2 map

PCA reports error in eigen (crossprod (t (x), t (x)), symmetric = true): 'x' has infinite value or missing value

你能读懂这个故事吗?

Three ways to dynamically call WebService.Net

Jerry ac1082/1074/1090 development record

JVM performance tuning

Can‘t use an undefined value as an ARRAY reference at probe2symbol
随机推荐
MySQL面试题大全(陆续更新)
漫谈测试平台—建设模式探讨
从非儿童网站看基线安全到底有多重要
PyTorch中grid_sample的使用方法
电工学下册自学笔记1.23
分支与循环(for与do-while)
hgu95av2.在线安装失败
QT编写串口助手
软件测试需要学习多久?
[阅读笔记] For Paper:R-CNN系列的三篇论文总结
leetcode系统性刷题(一)-----链表、栈、队列、堆
解决Package is not available (for R ve【PACKAGE ‘XXX’ IS NOT AVAILABLE (FOR R VERSION X.Y.Z)” WARNING?】
R语言画图/绘图/作图2
100+ collection of medical image data sets
关于非递归和递归分别实现求第n个斐波那契数
多大适合学习软件测试?
Mysql 优化总结
[阅读笔记] For:Object Detection with Deep Learning: The Definitive Guide
Esp-mqtt-at instruction connects Alibaba cloud Internet of things platform
easyui tree
