Es6异步(async+await)+模块化

119 阅读1分钟

async+await:

// 定时器+异步问题+延迟工作
    function show(time){
          return new Promise((resolve,reject)=>{
              setTimeout(()=>{
                  resolve(time*2)
              },time);
          })
      }
      function step(time){
          console.log(`正在处理中${time}`)
          return show(time);
      }
    async  function work(){
          let time1=1000;
        //   step(tiem1)
        //   .then(tiem2=>step(tiem2))
        //   .then(tiem3=>step(tiem3))
        //   .then(res=>{
        //          console.log("工作完成")
        //        })
      let time2 = await step(time1); //第一步
      let time3 = await step(time2); //第二步
      let time4 = await step(time3);//第三步
      console.log("工作结束")
      }
      work();

效果:

image.png

模块化:
3个页面(1个html、2个js)

image.png

模块化html:

image.png

export.js页面:

export function sum(a,b){
    console.log(a+b);

}
export function plus(a){
    console.log(a*a);
}
export let add=100;

image.png

import.js页面:

import { sum,plus ,add} from "./08export.js";

sum(10,30);
plus(add);

image.png