美文网首页
获取promise 状态

获取promise 状态

作者: 安石0 | 来源:发表于2022-01-18 22:21 被阅读0次
    function promiseState(p) {
      const t = {};
      return Promise.race([p, t])
        .then(v => (v === t)? "pending" : "fulfilled", () => "rejected");
    }
    
    var a = Promise.resolve();
    var b = Promise.reject();
    var c = new Promise(() => {});
    
    promiseState(a).then(state => console.log(state)); // fulfilled
    promiseState(b).then(state => console.log(state)); // rejected
    promiseState(c).then(state => console.log(state)); // pending
    

    相关文章

      网友评论

          本文标题:获取promise 状态

          本文链接:https://www.haomeiwen.com/subject/rvelhrtx.html