Baidu t7 course es6 learning notes 04 generator supplementary use

Posted by sgboise on Tue, 31 Dec 2019 13:12:43 +0100

function  * show(a1){
			// This is the starting material for cooking! It's easy to understand how to type a1 and b1
			// console.log(a1)
					
			// htmlobj=$.ajax({url:"/Readme.txt",async:false});
			
			// A is also a Promise object!!
			let a = $.ajax({url:'1.txt',dataType:'text',async:true})
			let  c = yield a;
			// Print the value passed in 
			console.log(c)
			// Perform step 2
			let  data2 =  $.ajax({url:'2.txt',dataType:'text',async:true})
			let  c2 = yield  $.ajax({url:'2.txt',dataType:'text',async:true})

			// The third step is to write at will!...
			// That is to say, how many asynchronies you can kick more!!
			return 100;
		}	


		oBtn.onclick = function(){
				//1. Get generator function object
				let a = show('action');

				// 2, kick one foot.
				let result1 = a.next()
				
				result1.value.then((str2)=>{
						console.log(str2)
						//Accept asynchronous results
						// A kick!
						let data2 = a.next('hello');
						data2.value.then((st22)=>{
								console.log(st22)
						})	
				})	
		}
}
	

Is generator used above, but when it is used, or is it more difficult to write? Somehow? Let me see?

 

 

I've finally learned that it's no wonder that there's no executable function, runner.js!

	<script type="text/javascript" src="runner.js"></script>

    function  * show(a1){
		

			let  c = yield $.ajax({url:'1.txt',dataType:'text',async:true});
			console.log("1" + c)
			let  data2 = yield $.ajax({url:'2.txt',dataType:'text',async:true})
			console.log("2" +  data2)	
			return 100;
		}	


		oBtn.onclick = function(){
			runner(show)	
		}

The operation structure is as follows:

In other words, when you need to perform multiple asynchronous operations, use runner to point to asynchronous operations!

 

async:

var asy  = async function(){
		
			let  c = await $.ajax({url:'1.txt',dataType:'text',async:true});
			console.log("1" + c)
			let  data2 = await $.ajax({url:'2.txt',dataType:'text',async:true})
			console.log("2" +  data2)	
			return 100;
		}	


		oBtn.onclick = function(){
			asy();
		}
}

OK, it's almost over. As for the details! I think it's the next one. Let's watch it carefully!

 

Topics: Javascript