Jade Dungeon

Javascript

Async

Promis

function async(callback, time) {
	return new Promise((resolve, reject) => {
		callback();
		setTimeout(() => { resolve(); }, time);
	});
};

async(() => { 
	console.log("step 1"); 
}, 3000).then(() => { return async(() => { 
	console.log("step 2"); 
}, 3000) }).then(() => { return async(() => { 
	console.log("step 3"); 
}, 3000) }).then(() => { return async(() => { 
	console.log("step 4"); 
}, 3000) }).then(() => { return async(() => { 
	console.log("step 5"); }) 
}).then(() => { return async(() => { 
	console.log("step 6"); 
}) });

动态加载代码

var script: String = 'module.exports = ' + 
	'async function waitForString(ms) { ' + 
		'    return new Promise((resolve, reject) => { ' + 
				'        setTimeout(resolve, ms, "done"); }); ' + 
			'};';
const process  = util.StringModuleLoader(script, "testModule");
const processOuter  = async () => {
    var ret = await process("test message");
    console.log(ret);
}
processOuter();

学习:

  • https://zhuanlan.zhihu.com/p/144058361
  • https://zhuanlan.zhihu.com/p/145442030