• promise你懂了吗?


    你能答对几题?

    题目一

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

    题目二

    const promise1 = new Promise((resolve, reject) => {
      setTimeout(() => {
        resolve('success')
      }, 1000)
    })
    const promise2 = promise1.then(() => {
      throw new Error('error!!!')
    })
    
    console.log('promise1', promise1)
    console.log('promise2', promise2)
    
    setTimeout(() => {
      console.log('promise1', promise1)
      console.log('promise2', promise2)
    }, 2000)

    题目三

    const promise = new Promise((resolve, reject) => {
      resolve('success1')
      reject('error')
      resolve('success2')
    })
    
    promise
      .then((res) => {
        console.log('then: ', res)
      })
      .catch((err) => {
        console.log('catch: ', err)
      })

    题目四

    Promise.resolve(1)
      .then((res) => {
        console.log(res)
        return 2
      })
      .catch((err) => {
        return 3
      })
      .then((res) => {
        console.log(res)
      })

    题目五

    const promise = new Promise((resolve, reject) => {
      setTimeout(() => {
        console.log('once')
        resolve('success')
      }, 1000)
    })
    
    const start = Date.now()
    promise.then((res) => {
      console.log(res, Date.now() - start)
    })
    promise.then((res) => {
      console.log(res, Date.now() - start)
    })

    题目六

    Promise.resolve()
      .then(() => {
        return new Error('error!!!')
      })
      .then((res) => {
        console.log('then: ', res)
      })
      .catch((err) => {
        console.log('catch: ', err)
      })

    题目七

    const promise = Promise.resolve()
      .then(() => {
        return promise
      })
    promise.catch(console.error)

    题目八

    Promise.resolve(1)
      .then(2)
      .then(Promise.resolve(3))
      .then(console.log)

    题目九

    Promise.resolve()
      .then(function success (res) {
        throw new Error('error')
      }, function fail1 (e) {
        console.error('fail1: ', e)
      })
      .catch(function fail2 (e) {
        console.error('fail2: ', e)
      })

    题目十

    process.nextTick(() => {
      console.log('nextTick')
    })
    Promise.resolve()
      .then(() => {
        console.log('then')
      })
    setImmediate(() => {
      console.log('setImmediate')
    })
    console.log('end')

    总结:promise的状态一旦确定的话,就不能修改.
    then()的参数如果不合法,则跳过
    promise构造函数的参数是个立即执行的函数,then是异步的

  • 相关阅读:
    win7 iis7 配置如何让局域网内其它用户访问
    WPF依赖项属性详解一
    项目管理SOW(工作说明书)
    新建silverlight 4 应用程序时出错:需要更新 microsoft visual studio 以面向 silverlight4
    WPF触发器(trigger)详解
    public,private,protected,internal作用
    抽象类,密封类及类成员
    java8 lanbda表达式对list对象去重,对象属性转成string
    window10环境ngnix配置https记录
    java动态调用webservice(.asmx?wsdl)
  • 原文地址:https://www.cnblogs.com/honghong87/p/9064694.html
Copyright © 2020-2023  润新知