Callback to Hell

Callback to Hell

The same is the operation of reading files, because reading files is an asynchronous operation, so reading files does not output in the order of code.

var fs = require('fs')

fs.readFile('./a.txt','utf8',function(err,data){
	if(err){
		throw err
	}console.log(data)
})

fs.readFile('./b.txt','utf8',function(err,data){
	if(err){
		throw err
	}console.log(data)
})

fs.readFile('./c.txt','utf8',function(err,data){
	if(err){
		throw err
	}console.log(data)
})

[failed to transfer the external link picture (img-p3z0tt9o-1564741746893) (C: \ users \ 14331 \ appdata \ roaming \ typora \ typora user images \ 1564382564978. PNG))

If you want to achieve sequential reading, you need to use code nesting. At this time, the execution process must wait for the output of a before reading b. Only after the output of b can you read c, that is, callback hell. Place b in the callback function of a and C in the callback function of b.

var fs = require('fs')

fs.readFile('./a.txt','utf8',function(err,data){
	if(err){
		throw err
	}console.log(data)
	fs.readFile('./b.txt','utf8',function(err,data){
	if(err){
		throw err
	}console.log(data)
	
fs.readFile('./c.txt','utf8',function(err,data){
	if(err){
		throw err
	}console.log(data)
})
})
})

[Img-enrEUQxy-15641746896 (C: Users 1431 AppData Roaming Typora typora-user-images 15387;97972.png)]

Using promise method to solve the problem

[Img-PcV76fNz-15641746899 (C: Users 1431 AppData Roaming Typora typora-user-images 70606.png)]

[Img-YD3KwmEP-15641746900 (C: Users 141;AppData Roaming Typora typora-user-images 3232png)]

var fs = require('fs')

//new Promise() creates a promise container, which is ES6's syntax and is automatically invoked when it is created
//The promise container has three states. The first is pending executing state, jejected has failed state, and resolved has succeeded state.
var p1 = new Promise(function(resolve,reject){
	fs.readFile('./a.txt','utf8',function(err,data){
	if(err){
		reject(err)
	}else{
		resolve(data)//After success, execute then,function(data)=resolve(data)
	}
})
})

var p2 = new Promise(function(resolve,reject){
	fs.readFile('./b.txt','utf8',function(err,data){
	if(err){
		reject(err)
	}else{
		resolve(data)
	}
})
})

var p3 = new Promise(function(resolve,reject){
	fs.readFile('./c.txt','utf8',function(err,data){
	if(err){
		reject(err)
	}else{
		resolve(data)
	}
})
})


//Data at this point is to read the successfully returned data. The first parameter is resolution (data), and the second parameter is reject(err).
p1.then(
	function(data){
	console.log(data)
	return p2//The p2 instance returned at this time
	},
	function(err){
	console.log('Failed to read the file',err)
}).then(
	function(data){
	console.log(data)
	return p3//The p3 instance returned at this time
	},
	function(err){
	console.log('Failed to read the file',err)
}).then(
	function(data){
	console.log(data)
	},
	function(err){
	console.log('Failed to read the file',err)
})

Encapsulation promise method

var fs = require('fs')

//new Promise() creates a promise container, which is ES6's syntax and is automatically invoked when it is created
//The promise container has three states. The first is pending executing state, jejected has failed state, and resolved has succeeded state.
function  Preadfile(pathfile){
	return new Promise(function(resolve,reject){
	fs.readFile(pathfile,'utf8',function(err,data){
	if(err){
		reject(err)
	}else{
		resolve(data)
	}
})
})
}



Preadfile('./a.txt')
	.then(function(data){
	console.log(data)
	return Preadfile('./b.txt')
}).then(function(data){
	console.log(data)
	return Preadfile('./c.txt')
}).then(function(data){
	console.log(data)
})

Posted by SmokyBarnable on Fri, 11 Oct 2019 10:57:56 -0700