简单的 async , await例子


var count = 0;
var tick = () => {
	count++;
	setTimeout(() => {
		process.stdout.write('.');
		if(count < 100) tick();
	},100);
}
tick();//Init


var getApple = (n) => {
	return new Promise((resolved,rejected) => {
		setTimeout(() => {
			resolved(`Apple x ${n}`);
		},2000)
	})
}
	

var doStuff = async () => {
	
	console.log('Going to get an apple ... ');
	var apples = await getApple(2);
	console.log('Got myself ' + apples);
	
	//Always return a promise?
	
}

doStuff();

console.log('This should be executed second.');

Output:


node index.js
Going to get an apple ...
This should be executed second.
...................Got myself Apple x 2
.................................................................................