js异步改同步执行
最简单实用的ES7新语法
示例代码:
function getSomething() {//js代码 var r = 0; return new Promise(function(resolve) { setTimeout(function() { r = 2; resolve(r); }, 10); }); } async function compute() { var x = await getSomething(); alert(x * 2); }
利用html2canvas的js前台转图片的base码,同步获取base码的值
应用实例:
function getPicBase() { let container = document.getElementById("pic")//html的元素id let imgData; let options ={ backgroundColor : '#060d19' } var path=html2canvas(container,options).then(canvas => { var imgData = canvas.toDataURL() /* let link = document.createElement('a') link.href = imgData link.download = 'maowei2.png' console.log(link) link.click()*///这段用于浏览器上下载图片 return imgData }) return path//path是一个promise对象,异步的 $.ajax()返回的也是一个promise对象 } test() async function test() { console.log(await getPicBase()) }