es6:Promise、await和promise

Promise本身是同步的,但是then是异步的

 let res = new Promise((res, rej) => {
      console.log(1);
    });
    console.log(2);
  },

Promise的一些方法

//all全都执行后才返回,any只要一个执行就返回
Promise.all([fn1,fn2]).then(res=>{console.log(res[1],res2)});

//race只有一个成功返回就结束  赛跑

 

promise的优缺点:

【缺点】:

1.转态一经进行,不得中止或更改

2.当时pending状态是无法得知处于哪个转态

3.从Es6诞生,向下兼容性差

 

await 和 promise

async修饰的函数默认返回一个Promise对象resolve的值

await是等待,意思是等待一起执行完再进行下一步

posted @ 2022-06-02 13:31  lxq3280  阅读(65)  评论(0)    收藏  举报