0
点赞
收藏
分享

微信扫一扫

前端_Promise面试题 3

穿裙子的程序员 2022-02-13 阅读 64
前端

题目一

    new Promise((resolve, reject) => {
      resolve(1)
    }).then(
      value => {
        console.log('onResolved1()', value)
        // 相当于return undefined
      },
      reason => {
        console.log('onRejected1()', reason)
      }
    ).then(
      value => {
        console.log('onResolved2()', value)
      },
      reason => {
        console.log('onRejected2()', reason)
      }
    )

 


    new Promise((resolve, reject) => {
      reject(1)
    }).then(
      value => {
        console.log('onResolved1()', value)
      },
      reason => {
        console.log('onRejected1()', reason)
        // 相当于return undefined
      }
    ).then(
      value => {
        console.log('onResolved2()', value)
      },
      reason => {
        console.log('onRejected2()', reason)
      }
    )

 


    new Promise((resolve, reject) => {
      resolve(1)
    }).then(
      value => {
        console.log('onResolved1()', value)
        return 2
      },
      reason => {
        console.log('onRejected1()', reason)
      }
    ).then(
      value => {
        console.log('onResolved2()', value)
      },
      reason => {
        console.log('onRejected2()', reason)
      }
    )

 


    new Promise((resolve, reject) => {
      resolve(1)
    }).then(
      value => {
        console.log('onResolved1()', value)
        return Promise.resolve(2)
      },
      reason => {
        console.log('onRejected1()', reason)
      }
    ).then(
      value => {
        console.log('onResolved2()', value)
      },
      reason => {
        console.log('onRejected2()', reason)
      }
    )

 


    new Promise((resolve, reject) => {
      resolve(1)
    }).then(
      value => {
        console.log('onResolved1()', value)
        return Promise.reject(2)
      },
      reason => {
        console.log('onRejected1()', reason)
      }
    ).then(
      value => {
        console.log('onResolved2()', value)
      },
      reason => {
        console.log('onRejected2()', reason)
      }
    )

 


    new Promise((resolve, reject) => {
      resolve(1)
    }).then(
      value => {
        console.log('onResolved1()', value)
        throw 2
      },
      reason => {
        console.log('onRejected1()', reason)
      }
    ).then(
      value => {
        console.log('onResolved2()', value)
      },
      reason => {
        console.log('onRejected2()', reason)
      }
    )

 


题目二

    new Promise((resolve, reject) => {
      setTimeout(() => {
        console.log('执行任务1(异步)');
        resolve(1)
      }, 1000);
    }).then(
      value => {
        console.log('任务1的结果:', value);
        console.log('执行任务2(同步)');
        return 2
      }
    ).then(
      value => {
        console.log('任务2的结果:', value);
        return new Promise((resolve, reject) => {
          // 启动任务3(异步)
          setTimeout(() => {
            console.log('执行任务3(异步)');
            resolve(3)
          }, 1000);
        })
      }
    ).then(
      value => {
        console.log('任务3的结果:', value);
      }
    )

 


题目三

    new Promise((resolve, reject) => {
      reject(1)
    }).then(
      value => {
        console.log('onResolved1()', value);
        return 2
      },
      // 默认写了
      // reason => {throw reason}
      // 相当于
      // reason => Promise.reject(reason)
    ).then(
      value => {
        console.log('onResolved2()', value);
        return 3
      },
      // 默认写了
      // reason => {throw reason}
      // 相当于
      // reason => Promise.reject(reason)
    ).catch(
      reason => {
        console.log('onRejected1()', reason);
      }
    )


题目四

    new Promise((resolve, reject) => {
      reject(1)
    }).then(
      value => {
        console.log('onResolved1()', value);
        return 2
      },
    ).catch(
      reason => {
        console.log('onRejected1()', reason);
        return new Promise(() => {}) // 返回一个pending的Promise就可以中断
      }
    ).then(
      value => {
        console.log('onResolved()', value);
      },
      reason => {
        console.log('onRejected()', reason);
      }
    )

  

举报

相关推荐

0 条评论