当前位置:网站首页>Use of promise object in ES6 asynchronous programming
Use of promise object in ES6 asynchronous programming
2022-07-19 16:04:00 【RemoteDev】
Promise Characteristics of the object :
1. Promise The state of the object is not affected by the outside world ,
One Promise The object represents an asynchronous operation .
One Promise Objects have 3 States :
Pending ---> Have in hand
Fulfilled ---> Have succeeded
Rejected ---> Failed
2. The status is changed only once , Once changed, it will not change again , It's going to stay that way (Resolved)
Pending ------> Fulfilled
Pending ------> Rejected
3. shortcoming : Cannot cancel an executing Promise, If no callback function is set , Internal errors cannot be thrown to the outside
Promise usage :
let onePromise = new Promise((resolve,reject)=>{
// Execute the code that requires asynchronous operation here
});
onePromise.then(
// Successful callback
(data)=>{
},
// Failed callback
(err)=>{
});
// @ts-ignore
let onePromise = new Promise(function(resolve, reject){
// Do some asynchronous operations
setTimeout(function(){
console.log(' Execution completed Promise');
resolve(' The data to be returned can be any data, such as the data returned by the interface ');
}, 2000);
});
// Handle Promise Execution results
onePromise.then(value => {
console.log(value);
},error=>{
});
let promise=new Promise((resolve,reject)=>{
for (let i = 0; i < 1000; i++) {
console.log(i,' Asynchronous task execution ...');
if(i===999){
resolve({'index':i,data:' Cycle execution is complete '});
}
}
reject();
});
promise.then((data)=>{
console.log(data);
},(error)=>{
});// The function returns Promise object
function runPromise(){
// @ts-ignore
return new Promise((resolve,reject)=>{
// Asynchronous execution
for (let i = 0; i < 10000; i++) {
console.log(i);
}
resolve('Loop Finish');
});
}
//Promise Callback
runPromise().then((value => {
console.log(' Asynchronous execution complete :',value);
}));// Asynchronous acquisition JSON
let getJsonAsync = (url)=>{
// @ts-ignore
let p=new Promise((resolve,reject)=>{
let c= new XMLHttpRequest();
c.open('GET',url);
c.onreadystatechange = handler;
c.responseType='json';
c.setRequestHeader('Accept','application/json');
c.send();
// Handle request status events
var handler = ()=>{
if(this.readyState !== 4){
return;
}
// @ts-ignore
if(this.status === 200){
resolve(this.response);
}else{
reject(new Error(this.statusText));
}
};
});
return p;
};
getJsonAsync('https://img-home.csdnimg.cn/data_json/toolbar/toolbar1105.json').then((json)=>{
console.log(json);
},(err)=>{
console.log('HTTP Request error :',err);
});
边栏推荐
- wpa_ supplicant @authenticate
- C language simple version address book (static version)
- Liquibase学习 - 问题解决:启动报错
- [MCU simulation project] advertising lamp (Proteus schematic +keil code)
- Liquibase学习2 - 拓展插件liquibase-data介绍
- Typescript中对象扩展之动态添加方法与属性修改
- 第一个接受素数定理的人
- Liquibase学习3 - logLevel日志等级、常用命令
- ES6异步编程之Promise对象使用
- Wpa_ Supplicant WiFi connection
猜你喜欢

几行代码,让黑白老照片重获新生!

群里的初级工程师求助说,要采集采招数据,必须给他安排上

LVS load balancing cluster

The use of "!" in vscode is invalid, and there is no solution to the template problem
![[dry goods] how much do you know about MySQL infrastructure design?](/img/d0/de3837588ce6cfc04556d186395612.png)
[dry goods] how much do you know about MySQL infrastructure design?
uni-app进阶之自定义【day13】

Why is Emoji called Emoji

Excel文件数据导入到MySQL数据库

开源WPF控件库-AdonisUI

【干货】MySQL底层架构设计,你了解多少?
随机推荐
Is it reliable to open a new bond with one click? Is it really safe
Excel文件数据导入到MySQL数据库
CoCon: A Self-Supervised Approach for Controlled Text Generation | ICLR 2021
windows安装mysql
Woman Yelling At a Cat
数学建模--从自然走向理性之路--数学建模概论(学习笔记1)
uni-app进阶之自定义【day13】
Stock financial information, board of directors, board of supervisors and other senior management information crawling
群里的初级工程师求助说,要采集采招数据,必须给他安排上
如何正确重写hashCode方法?
读书笔记:程序员的自我修养---第二章
Interference signal radiated from motor
【干货】MySQL底层架构设计,你了解多少?
Mogdb/opengauss permission collation
About the problem that only one IP can be accessed after dual network cards are configured
Mysql 温故知新系列「触发器详解」
Qt | 控件之QComboBox
LVS负载均衡群集
[projet de simulation MCU] feu d'alarme (schéma Proteus + Code clé)
项目实战第二十一讲:平台商品库