Promise(面试题)

327 阅读2分钟

Promise 有几种状态,什么时候会进入catch?

Promise 有几种状态

  • 三个状态: pending(对象实例创建时的初始状态)、fulfilled(成功的状态)、reject(失败的状态)

  • 两个过程: padding -> fulfilledpadding -> reject Promise 什么时候会进入 catch

  • pendingrejected 时,会进入 catch

Promiuse 中 reject 和 catch 处理上有什么区别

  1. reject 是用来抛出异常, catch 是用来处理异常
  2. rejectPromise 的方法,而 catchPromise 实例的方法
  3. reject 后的东西,一定会进入 then 中的第二个回调,如果 then 中没有写第二个回调,则进入 catch
  4. 网络异常(不如断网),会直接进入 catch 而不会进入 then 的第二个回调

手写一个 Promise

        var Promise = new Promise((resolve, reject) => {
          if (操作成功) {
            resolve(value);
          } else {
            reject(error);
          }
        });
        Promise.then(
          function (value) {
            // success
          },
          function (value) {
            //failure
          }
        );

自己模拟一个

        function MyPromise(fn){
          this.callBackFnArr = [] // 2.用来装 then 里面的回调函数
          const resolve = (value) =>{
            setTimeout(() =>{ //6.在这里改装,确保在 then调用后,再执行这里
              this.callBackFnArr.map(v => v(value))//5.遍历数组里 then里的会带偶函数执行(注意代码执行顺序,这时候 pro.then()还没有执行)
            })
          }
          fn(resolve)
        }
        MyPromise.prototype.then = function(thenFn){//1.定义 then方法,把 then 里要执行的函数加入到数组中
          this.callBackFnArr.push(thenFn)
        }
        let pro = new MyPromise(resolve =>{ //3.这里的回调函数马上执行
          resolve(123) //4.调用4行内部 resolve 触发
        })
        pro.then(result =>{ //此时还没有添加 then函数,上面就走完了
          console.log(result)
        })

下面的输出结果是多少

(1)

      const Promise = new Promise((resolve, reject) => {
        console.log(2)
        resolve()
        console.log(333)
      })
      Promise.then(() => {
        console.log(666)
      })
      console.log(888)

解析:

       Promise 新建后立即执行,所以会先输出2,333,而 Promise.then() 内部的代码在当次事件循环的结尾立即执行,所以会继续输出888,最后输出 666

(2)

      setTimeout(function () {
        console.log(1)
      }, 0)
      new Promise(function executor(resolve) {
        console.log(2)
        for (var i = 0; i < 1000; i++) {
          i = 9999 && resolve()
        }
        console.log(3)
      }).then(function () {
        console.log(4)
      });
      console.log(5)

解析:

       首先先碰到一个 satTimeout,于是会先设置一个定时,在定时结束后将传递这个函数放到任务队列里面,因此开始一定不会输出 1。然后是一个 Promise,里面的函数是直接执行的,因此应该直接输出2 3。最后,Promisethen 应当回访到当前 tick 的最后,但是还是在当前 tick 中,所以,应当先出输出 5,然后再输出 4,最后再到下一个 tick,就是 1。

(3)

      const Promise = new Promise((resolve, reject) => {
        console.log(1)
        resolve()
        console.log(2)
      })
      Promise.then(() => {
        console.log(3)
      })
      console.log(4)

解析:

       Promise 构造函数是同步执行,Promise.then 中的函数是异步执行的,所以运行结果为:1 2 4 3