当前位置:网站首页>Promise optimized callback hell
Promise optimized callback hell
2022-07-02 22:07:00 【Brave * Niuniu】
promise
callback hell- Back to hell

var fs = require('fs')
fs.readFile('./data/a.text','uft8',function(err,data){
if(err){
throw err
}
console.log(data);
})
fs.readFile('./data/b.text','uft8',function(err,data){
if(err){
throw err
}
console.log(data);
})
fs.readFile('./data/c.text','uft8',function(err,data){
if(err){
throw err
}
console.log(data);
})
The order is guaranteed by nesting callbacks
var fs = require('fs')
fs.readFile('./data/a.text','uft8',function(err,data){
if(err){
throw err
}
console.log(data);
fs.readFile('./data/b.text','uft8',function(err,data){
if(err){
throw err
}
console.log(data);
fs.readFile('./data/c.text','uft8',function(err,data){
if(err){
throw err
}
console.log(data);
})
})
})
When nested too deeply , Difficult to maintain
In order to solve the problem solved by the above coding method, so es6 There's a new API
promise
Commitment is not asynchronous , Content ( Mission ) It's asynchronous
Basic grammar
// Create a container
// 1、 Give someone a promise
var p1 = new Promise(function (resolve, reject) {
fs.readFile('./data/a.text', 'utf8', function (err,data) {
if (err) {
// A change of state
reject(err)
} else {
// success
resolve(data)
}
})
})
// p1 It's commitment
// then Method of acceptance funtion It's in the container resolve
p1
.then(function(data){
console.log(data);
},function(err){
console.log(' Read failed ');
})
example
var fs = require('fs')
var p1 = new Promise(function (resolve, reject) {
fs.readFile('./a.text', 'utf8', function (err, data) {
if (err) {
// A change of state
reject(err)
} else {
// success
resolve(data)
}
})
})
var p2 = new Promise(function (resolve, reject) {
fs.readFile('./b.text', 'utf8', function (err, data) {
if (err) {
// A change of state
reject(err)
} else {
// success
resolve(data)
}
})
})
var p3 = new Promise(function (resolve, reject) {
fs.readFile('./c.text', 'utf8', function (err, data) {
if (err) {
// A change of state
reject(err)
} else {
// success
resolve(data)
}
})
})
p1
.then(function (data) {
console.log(data);
// Returns the object
return p2;
}, function (err) {
console.log(' Read failed ');
})
.then(function (data) {
// Here it is p2 Of resolve function
console.log(data);
return p3;
})
.then(function (data) {
// Here it is p3 Of resolve function
console.log(data);
})
边栏推荐
- How to write a good program when a big book speaks every day?
- Leetcode theme [array] -169- most elements
- Scrcpy this software solves the problem of sharing mobile screen with colleagues | community essay solicitation
- MySQL inserts Chinese data and reports an error. Set the default collation
- Three chess games
- 【剑指 Offer】57. 和为s的两个数字
- The failure rate is as high as 80%. What should we do about digital transformation?
- "New programmer 003" was officially launched, and the cloud native and digital practical experience of 30+ companies such as Huawei and Alibaba
- 关于测试用例
- Evolution of messaging and streaming systems under the native tide of open source cloud
猜你喜欢

How is LinkedList added?

CVPR论文解读 | 弱监督的高保真服饰模特生成

D4: unpaired image defogging, self enhancement method based on density and depth decomposition (CVPR 2022)

Pip install whl file Error: Error: … Ce n'est pas une roue supportée sur cette plateforme

Introduction to the principle of geographical detector

MySQL learning record (3)

情感计算与理解研究发展概述

Technical solution of vision and manipulator calibration system

Reading experience of just because

The failure rate is as high as 80%. What should we do about digital transformation?
随机推荐
基本IO接口技术——微机第七章笔记
pip安裝whl文件報錯:ERROR: ... is not a supported wheel on this platform
Daily book - low code you must understand in the era of digital transformation
A week's life
Chargement de l'image pyqt après décodage et codage de l'image
Ransack combined condition search implementation
暑期第一周总结
[use of pointer and pointer and array]
Kubernetes resource object introduction and common commands (4)
[staff] Sibelius 7.5.1 score software installation (software download | software installation)
An overview of the development of affective computing and understanding research
[Yu Yue education] reference materials of analog electronic technology of Nanjing Institute of information technology
腾讯三面:进程写文件过程中,进程崩溃了,文件数据会丢吗?
How to write a good program when a big book speaks every day?
The web version of xshell supports FTP connection and SFTP connection [detailed tutorial] continued from the previous article
App page sharing password rails implementation
Three chess games
Detailed explanation of OSI seven layer model
*C language final course design * -- address book management system (complete project + source code + detailed notes)
Five message formats of OSPF