zoukankan      html  css  js  c++  java
  • vue中对api的管理,对异步最好处理

    /*
    async:异步 await 等待 等待上一个promise执行完毕以后才会执行下一个pormise 同时await还接收resolve中的参数

    语法 var fn = asnyc function(){}

    async最好配合promise一起使用
    */

    //var fn = async function(){} //当前函数是异步


    var fn1 = function(){
    return new Promise((resolve)=>{
    setTimeout(()=>{
    console.log(111);
    var obj = {name:1}
    resolve(obj);
    },2000)
    })
    }

    var fn2 = function(){
    return new Promise((resolve)=>{
    setTimeout(()=>{
    console.log(222)
    resolve();
    },1000)
    })
    }

    var fn3 = function(){
    return new Promise((resolve)=>{
    setTimeout(()=>{
    console.log(333)
    resolve();
    },500)
    })
    }


    // var p = new Promise(resolve=>{
    // setTimeout(()=>{
    // console.log(0000)
    // resolve();
    // },3000)
    // })

    // p.then(()=>{
    // return fn1();
    // })
    // .then(()=>{
    // return fn2();
    // })
    // .then(()=>{
    // fn3();
    // })


    // var test = async ()=>{
    // await fn1();
    // await fn2();
    // await fn3();
    // }
    // test();


    var test = async ()=>{
    let data = await fn1();
    console.log(data);
    }
    test();

  • 相关阅读:
    SpringBoot和SpringCould的关系
    MyBatis全局配置文件头
    MyBatis的SQL映射文件头
    MyBatis 驼峰式配置 yml配置
    频率组件
    序列化和反序列化
    生成器面试题
    序列化组件
    进程间通信IPC机制
    信号量、event事件和线程queue
  • 原文地址:https://www.cnblogs.com/PeiGaGa/p/11032692.html
Copyright © 2011-2022 走看看