【H5】Usage of Promise

series of articles

C# underlying library – record log helper
link to this article: https://blog.csdn.net/youcheng_ge/article/details/124187709


foreword

This column is [H5], which mainly introduces front-end knowledge points.
insert image description here

1. Technical introduction

CSV file and DataTable object conversion helper class. Our database export file is in "CSV" format, when you want to read "CSV" file, you can use this class library.

注意:请填写

2. Project source code

2.1 Promise state

Promise has three states: pending(准备,待定态)、fulfilled(已完成,成功态)、rejected(已拒绝,失败态), the state change of Promise is one-time.

	<script>
	const p = new Promise((resolve,reject)=>{
    
    
		// resolve()
		// reject()
	});
	console.dir(p);
	</script>
</html>

insert image description here

	<script>
	const p = new Promise((resolve,reject)=>{
    
    
		resolve()
		// reject()
	});
	console.dir(p);
	</script>

insert image description here

	<script>
	const p = new Promise((resolve,reject)=>{
    
    
		// resolve()
		reject()
	});
	console.dir(p);
	</script>

insert image description here

2.2 Results of Promises

PromiseResult result

	<script>
	const p = new Promise((resolve,reject)=>{
    
    
		resolve('成功的输出')
		// reject('失败的输出')
	});
	console.dir(p);
	</script>

insert image description here

2.3 then method parameters of Promise

The then method takes two parameters.
Parameters:
1. A function to receive value
2. Another function to receive reason or err
Return value: Promise object

	<script>
		const p = new Promise((resolve, reject) => {
    
    
			// resolve('成功的输出')
			reject('失败的输出')
		});
		p.then(() => {
    
    
			console.log('成功时执行');
		}, () => {
    
    
			console.log('失败时执行');
		});

		console.dir(p);
	</script>

insert image description here

2.4 The then method of Promise gets data

<script>
	const p = new Promise((resolve, reject) => {
    
    
		// resolve('123')
		reject('456')
	});
	p.then(value => {
    
    
		console.log('成功时执行:' + value);
	}, (reason) => {
    
    
		console.log('失败时执行' + reason);
	});

	console.dir(p);
</script>

insert image description here

3. Effect display

4. Resource link

Guess you like

Origin blog.csdn.net/youcheng_ge/article/details/131634038