当前位置:网站首页>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);
})
边栏推荐
- [leetcode] sword finger offer 04 Search in two-dimensional array
- ServiceMesh主要解决的三大痛點
- Service visibility and observability
- Landingsite eband B1 smoke test case
- APP页面分享口令Rails实现
- Free open source web version of xshell [congratulations on a happy new year]
- [sword finger offer] 56 - I. the number of numbers in the array
- 服务可见可观测性
- ServiceMesh主要解决的三大痛点
- Infrastructure is code: a change is coming
猜你喜欢
Introduction to the principle of geographical detector
Etcd Raft 协议
From personal heroes to versatile developers, the era of programmer 3.0 is coming
Infrastructure is code: a change is coming
Basic IO interface technology - microcomputer Chapter 7 Notes
《Just because》阅读感受
如何访问kubernetes API?
腾讯三面:进程写文件过程中,进程崩溃了,文件数据会丢吗?
Scrcpy this software solves the problem of sharing mobile screen with colleagues | community essay solicitation
MySQL learning record (9)
随机推荐
App page sharing password rails implementation
How to center the positioned text horizontally and vertically
Blue Bridge Cup Winter vacation homework (DFS backtracking + pruning)
加了定位的文字如何水平垂直居中
scrcpy这款软件解决了和同事分享手机屏幕的问题| 社区征文
Image segmentation using pixellib
Gbase8s database type
"New programmer 003" was officially launched, and the cloud native and digital practical experience of 30+ companies such as Huawei and Alibaba
Using emqx cloud to realize one machine one secret verification of IOT devices
Landingsite eband B1 smoke test case
MySQL learning record (2)
一周生活
"New programmer 003" was officially launched, and the cloud native and digital practical experience of 30+ companies such as Huawei and Alibaba
pip安裝whl文件報錯:ERROR: ... is not a supported wheel on this platform
Bridge emqx cloud data to AWS IOT through the public network
[shutter] shutter layout component (physicalmodel component)
Gbase 8s database basic syntax
The source code of the daily book analyzes the design idea of Flink and solves the problems in Flink
SQL必需掌握的100个重要知识点:管理事务处理
Leetcode theme [array] -169- most elements