当前位置:网站首页>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); });
边栏推荐
- Jerry ac692n --- prompt tone compression and modification
- 【C语言进阶】——剖析入微数据在内存中的存储 【下】(浮点数存储)
- An article takes you closer to the overview and principle of kubernetes
- Vscode intranet access server
- [阅读笔记]-2 通过朴素贝叶斯模型学习机器学习分类
- Precautions for $ionicpopup in ionic when calling alert for two consecutive times
- Factor in R
- 编译原理学习笔记2(语法分析介绍)
- 在airtest中使用ocr反向识别文本内容
- 多大适合学习软件测试?
猜你喜欢

leetcode系统性刷题(一)-----链表、栈、队列、堆

MySQL高级-MVCC(超详细整理)

Management of third-party technical services in product development

【C语言笔记分享】——动态内存管理malloc、free、calloc、realloc、柔性数组

Can you read the story?

一篇带你走近Kubernetes概貌与原理

easyui tree

Factor in R

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

In depth sharing of Ali (ant financial) technical interview process, with preliminary preparation and learning direction
随机推荐
软件测试工作内容太简单怎么办?
Factor in R
Alibaba P8 architect talk: seven knowledge points (including interview questions) that must be learned well to become an architect
Mqtt.fx connects to Alibaba cloud Internet of things platform
软件测试干货
[阅读笔记] For Paper:R-CNN系列的三篇论文总结
The browser has no Internet, and wechat can connect to the Internet (solution)
软件测试就业前景如何?
An article takes you closer to the overview and principle of kubernetes
Students' 20 R language exercises
Division optimization of JS decimal calculation on the Internet
JDWP未授权快速利用
R language drawing / drawing / drawing 2
编译原理学习笔记3(自上而下语法分析)
Kali installation configuration of penetration test killer
零基础学习软件测试有什么条件?
蚂蚁金服移动测试工具solopi监控部分源码导读。。持续更新
谈谈你知道的发布上线(二)
漫谈测试平台—平台建设思路(上)
在airtest中使用ocr反向识别文本内容
