js常见面试题——详解Promise使用与原理及实现过程(附源码)

先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前阿里P7

深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
img
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上Java开发知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

如果你需要这些资料,可以添加V获取:vip1024b (备注Java)
img

正文

this.onRejectedFunctions = [] // 存放这个 promise 注册的 then 函数中传的第二个函数参数
const resolve = value => {
if (this.status === ‘pending’) {
this.value = value
this.status = ‘fulfilled’
this.onFulfilledFunctions.forEach(onFulfilled => {
onFulfilled() // 将 onFulfilledFunctions 中的函数拿出来执行
})
}
}
const reject = value => {
if (this.status === ‘pending’) {
this.value = value
this.status = ‘rejected’
this.onRejectedFunctions.forEach(onRejected => {
onRejected() // 将 onRejectedFunctions 中的函数拿出来执行
})
}
}
this.executor(resolve, reject)
}

then(onFulfilled, onRejected) {
const self = this
if (this.status === ‘pending’) {
/**

  • 当 promise 的状态仍然处于 ‘pending’ 状态时,需要将注册 onFulfilled、onRejected 方法放到 promise 的 onFulfilledFunctions、onRejectedFunctions 备用
    */
    return new MyPromise((resolve, reject) => {
    this.onFulfilledFunctions.push(() => {
    const thenReturn = onFulfilled(self.value)
    resolve(thenReturn)
    })
    this.onRejectedFunctions.push(() => {
    const thenReturn = onRejected(self.value)
    resolve(thenReturn)
    })
    })
    } else if (this.status === ‘fulfilled’) {
    return new MyPromise((resolve, reject) => {
    const thenReturn = onFulfilled(self.value)
    resolve(thenReturn)
    })
    } else {
    return new MyPromise((resolve, reject) => {
    const thenReturn = onRejected(self.value)
    resolve(thenReturn)
    })
    }
    }
    }

对于以上完成的 MyPromise 进行测试,测试代码如下

const p = new MyPromise((resolve, reject) => {
setTimeout(() => {
resolve(1)
}, 1000)
})

p.then(res => {
console.log(‘first then’, res)
return res + 1
}).then(res => {
console.log(‘first then’, res)
})

p.then(res => {
console.log(second then, res)
return res + 1
}).then(res => {
console.log(second then, res)
})

/**

  • 输出结果如下:
  • first then 1
  • first then 2
  • second then 1
  • second then 2
    */

(3)在 promise 相关的内容中,有一点常常被我们忽略,当 then 函数中返回的是一个 promise 应该如何处理?
考虑如下代码:

// 使用正确的 Promise
new Promise((resolve, reject) => {
setTimeout(() => {
resolve()
}, 1000)
})
.then(res => {
console.log(‘外部 promise’)
return new Promise((resolve, reject) => {
resolve(内部 promise)
})
})
.then(res => {
console.log(res)
})

/**

  • 输出结果如下:
  • 外部 promise
  • 内部 promise
    */

通过以上的输出结果不难判断,当 then 函数返回的是一个 promise 时,promise 并不会直接将这个 promise 传递到下一个 then 函数,而是会等待该 promise resolve 后,将其 resolve 的值,传递给下一个 then 函数,找到我们实现的代码的 then 函数部分,做以下修改:

then(onFulfilled, onRejected) {
const self = this
if (this.status === ‘pending’) {
return new MyPromise((resolve, reject) => {
this.onFulfilledFunctions.push(() => {
const thenReturn = onFulfilled(self.value)
if (thenReturn instanceof MyPromise) {
// 当返回值为 promise 时,等该内部的 promise 状态扭转时,同步扭转外部的 promise 状态
thenReturn.then(resolve, reject)
} else {
resolve(thenReturn)
}
})
this.onRejectedFunctions.push(() => {
const thenReturn = onRejected(self.value)
if (thenReturn instanceof MyPromise) {
// 当返回值为 promise 时,等该内部的 promise 状态扭转时,同步扭转外部的 promise 状态
thenReturn.then(resolve, reject)
} else {
resolve(thenReturn)
}
})
})
} else if (this.status === ‘fulfilled’) {
return new MyPromise((resolve, reject) => {
const thenReturn = onFulfilled(self.value)
if (thenReturn instanceof MyPromise) {
// 当返回值为 promise 时,等该内部的 promise 状态扭转时,同步扭转外部的 promise 状态
thenReturn.then(resolve, reject)
} else {
resolve(thenReturn)
}
})
} else {
return new MyPromise((resolve, reject) => {
const thenReturn = onRejected(self.value)
if (thenReturn instanceof MyPromise) {
// 当返回值为 promise 时,等该内部的 promise 状态扭转时,同步扭转外部的 promise 状态
thenReturn.then(resolve, reject)
} else {
resolve(thenReturn)
}
})
}
}

(4) 之前的 promise 实现代码仍然缺少很多细节逻辑,下面会提供一个相对完整的版本,注释部分是增加的代码,并提供了解释。

class MyPromise {
constructor(executor) {
this.executor = executor
this.value = null
this.status = ‘pending’
this.onFulfilledFunctions = []
this.onRejectedFunctions = []
const resolve = value => {
if (this.status === ‘pending’) {
this.value = value
this.status = ‘fulfilled’
this.onFulfilledFunctions.forEach(onFulfilled => {
onFulfilled()
})
}
}
const reject = value => {
if (this.status === ‘pending’) {
this.value = value
this.status = ‘rejected’
this.onRejectedFunctions.forEach(onRejected => {
onRejected()
})
}
}
this.executor(resolve, reject)
}

then(onFulfilled, onRejected) {
const self = this
if (typeof onFulfilled !== ‘function’) {
// 兼容 onFulfilled 未传函数的情况
onFulfilled = function() {}
}
if (typeof onRejected !== ‘function’) {
// 兼容 onRejected 未传函数的情况
onRejected = function() {}
}
if (this.status === ‘pending’) {
return new MyPromise((resolve, reject) => {
this.onFulfilledFunctions.push(() => {
try {
const thenReturn = onFulfilled(self.value)
if (thenReturn instanceof MyPromise) {
thenReturn.then(resolve, reject)
} else {
resolve(thenReturn)
}
} catch (err) {
// catch 执行过程的错误
reject(err)
}
})
this.onRejectedFunctions.push(() => {
try {
const thenReturn = onRejected(self.value)
if (thenReturn instanceof MyPromise) {
thenReturn.then(resolve, reject)
} else {
resolve(thenReturn)
}
} catch (err) {
// catch 执行过程的错误
reject(err)
}
})
})
} else if (this.status === ‘fulfilled’) {
return new MyPromise((resolve, reject) => {
try {
const thenReturn = onFulfilled(self.value)
if (thenReturn instanceof MyPromise) {
thenReturn.then(resolve, reject)
} else {
resolve(thenReturn)
}
} catch (err) {
// catch 执行过程的错误
reject(err)
}
})
} else {
return new MyPromise((resolve, reject) => {
try {
const thenReturn = onRejected(self.value)
if (thenReturn instanceof MyPromise) {
thenReturn.then(resolve, reject)
} else {
resolve(thenReturn)
}
} catch (err) {
// catch 执行过程的错误
reject(err)
}
})
}
}
}

(5)至此一个相对完整的 promise 已经实现,但他仍有一些问题,了解宏任务、微任务的同学一定知道,promise 的 then 函数实际上是注册一个微任务,then 函数中的参数函数并不会同步执行。
查看如下代码:

new Promise((resolve,reject)=>{
console.log(promise 内部)
resolve()
}).then((res)=>{
console.log(第一个 then)
})
console.log(promise 外部)

/**

  • 输出结果如下:
  • promise 内部
  • promise 外部
  • 第一个 then
    */

// 但是如果使用我们写的 MyPromise 来执行上面的程序

new MyPromise((resolve,reject)=>{
console.log(promise 内部)
resolve()
}).then((res)=>{
console.log(第一个 then)
})
console.log(promise 外部)
/**

  • 输出结果如下:
  • promise 内部
  • 第一个 then
  • promise 外部
    */

以上的原因是因为的我们的 then 中的 onFulfilled、onRejected 是同步执行的,当执行到 then 函数时上一个 promise 的状态已经扭转为 fulfilled 的话就会立即执行 onFulfilled、onRejected。
要解决这个问题也非常简单,将 onFulfilled、onRejected 的执行放在下一个事件循环中就可以了。

if (this.status === ‘fulfilled’) {
return new MyPromise((resolve, reject) => {
setTimeout(() => {
try {
const thenReturn = onFulfilled(self.value)
if (thenReturn instanceof MyPromise) {
thenReturn.then(resolve, reject)
} else {
resolve(thenReturn)
}
} catch (err) {
// catch 执行过程的错误
reject(err)

最后

面试前一定少不了刷题,为了方便大家复习,我分享一波个人整理的面试大全宝典

  • Java核心知识整理

2020年五面蚂蚁、三面拼多多、字节跳动最终拿offer入职拼多多

Java核心知识

  • Spring全家桶(实战系列)

2020年五面蚂蚁、三面拼多多、字节跳动最终拿offer入职拼多多

  • 其他电子书资料

2020年五面蚂蚁、三面拼多多、字节跳动最终拿offer入职拼多多

Step3:刷题

既然是要面试,那么就少不了刷题,实际上春节回家后,哪儿也去不了,我自己是刷了不少面试题的,所以在面试过程中才能够做到心中有数,基本上会清楚面试过程中会问到哪些知识点,高频题又有哪些,所以刷题是面试前期准备过程中非常重要的一点。

以下是我私藏的面试题库:

2020年五面蚂蚁、三面拼多多、字节跳动最终拿offer入职拼多多

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加V获取:vip1024b (备注Java)
img

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

哪些知识点,高频题又有哪些,所以刷题是面试前期准备过程中非常重要的一点。

以下是我私藏的面试题库:

[外链图片转存中…(img-2bQL8mgs-1713635525218)]

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加V获取:vip1024b (备注Java)
[外链图片转存中…(img-jETeo9re-1713635525219)]

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值