搜尽全网,整理了19道promise 面试题,你能做对几个?,GitHub标星3.2K

14 阅读4分钟

总结

三套“算法宝典”

28天读完349页,这份阿里面试通关手册,助我闯进字节跳动

算法刷题LeetCode中文版(为例)

人与人存在很大的不同,我们都拥有各自的目标,在一线城市漂泊的我偶尔也会羡慕在老家踏踏实实开开心心养老的人,但是我深刻知道自己想要的是一年比一年有进步。

最后,我想说的是,无论你现在什么年龄,位于什么城市,拥有什么背景或学历,跟你比较的人永远都是你自己,所以明年的你看看与今年的你是否有差距,不想做咸鱼的人,只能用尽全力去跳跃。祝愿,明年的你会更好!

由于篇幅有限,下篇的面试技术攻克篇只能够展示出部分的面试题,详细完整版以及答案解析,有需要的可以关注

开源分享:docs.qq.com/doc/DSmRnRG…

===

难易程度:⭐⭐

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)

})

#06

===

难易程度:⭐⭐⭐

Promise.resolve()

.then(() => {

return new Error('error!!!')

})

.then((res) => {

console.log('then: ', res)

})

.catch((err) => {

console.log('catch: ', err)

})

#07

===

难易程度:⭐⭐⭐⭐

const promise = Promise.resolve()

.then(() => {

return promise

})

promise.catch(console.error)

#08

===

难易程度:⭐⭐⭐

Promise.resolve(1)

.then(2)

.then(Promise.resolve(3))

.then(console.log)

#09

===

难易程度:⭐⭐⭐

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)

})

变种后

Promise.resolve()

.then(function success1 (res) {

throw new Error('error')

}, function fail1 (e) {

console.error('fail1: ', e)

})

.then(function success2 (res) {

}, function fail2 (e) {

console.error('fail2: ', e)

})

#10

===

难易程度:⭐⭐⭐⭐

process.nextTick(() => {

console.log('nextTick')

})

Promise.resolve()

.then(() => {

console.log('then')

})

setImmediate(() => {

console.log('setImmediate')

})

console.log('end')

#11

===

难易程度:⭐⭐⭐⭐

const first = () => (new Promise((resolve, reject) => {

console.log(3);

let p = new Promise((resolve, reject) => {

console.log(7);

setTimeout(() => {

console.log(5);

resolve(6);

}, 0)

resolve(1);

});

resolve(2);

p.then((arg) => {

console.log(arg);

});

}));

first().then((arg) => {

console.log(arg);

});

console.log(4);

#12

===

难易程度:⭐⭐

var p = new Promise((resolve, reject) => {

reject(Error('The Fails!'))

})

p.catch(error => console.log(error.message))

p.catch(error => console.log(error.message))

#13

===

难易程度:⭐⭐⭐

var p = new Promise((resolve, reject) => {

return Promise.reject(Error('The Fails!'))

})

p.catch(error => console.log(error.message))

p.catch(error => console.log(error.message))

#14

===

难易程度:⭐⭐

var p = new Promise((resolve, reject) => {

reject(Error('The Fails!'))

})

.catch(error => console.log(error))

.then(error => console.log(error))

#15

===

难易程度:⭐⭐

new Promise((resolve, reject) => {

resolve('Success!')

})

.then(() => {

throw Error('Oh noes!')

})

.catch(error => {

return "actually, that worked"

})

.catch(error => console.log(error.message))

#16

===

难易程度:⭐⭐

Promise.resolve('Success!')

.then(data => {

return data.toUpperCase()

})

.then(data => {

console.log(data)

return data

})

.then(console.log)

#17

===

难易程度:⭐⭐

Promise.resolve('Success!')

.then(() => {

throw Error('Oh noes!')

})

.catch(error => {

return 'actually, that worked'

})

.then(data => {

throw Error('The fails!')

})

.catch(error => console.log(error.message))

#18

===

难易程度:⭐⭐⭐⭐

const first = () => (new Promise((resolve,reject)=>{

console.log(3);

let p = new Promise((resolve, reject)=>{

console.log(7);

setTimeout(()=>{

console.log(5);

resolve(6); 

},0)

resolve(1);

}); 

resolve(2);

p.then((arg)=>{

console.log(arg);

});

}));

first().then((arg)=>{

console.log(arg);

});

console.log(4);

#19

===

难易程度:⭐⭐⭐⭐⭐

async function async1() {

console.log(1);

const result = await async2();

console.log(3);

}

async function async2() {

console.log(2);

}

Promise.resolve().then(() => {

console.log(4);

});

setTimeout(() => {

console.log(5);

});

async1();

console.log(6);

===

# 最后

=====

以上19个代码题未贴答案,后面会单独发送。

也欢迎大家在留言区回复参与答题。

今天一提就到这里,希望对你有所帮助。

参考资料:

zhuanlan.zhihu.com/p/34421918

zhuanlan.zhihu.com/p/30797777

zhuanlan.zhihu.com/p/98164787

juejin.cn/post/684490…

学习笔记

主要内容包括html,css,html5,css3,JavaScript,正则表达式,函数,BOM,DOM,jQuery,AJAX,vue等等

开源分享:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】

HTML/CSS

**HTML:**HTML基本结构,标签属性,事件属性,文本标签,多媒体标签,列表 / 表格 / 表单标签,其他语义化标签,网页结构,模块划分

**CSS:**CSS代码语法,CSS 放置位置,CSS的继承,选择器的种类/优先级,背景样式,字体样式,文本属性,基本样式,样式重置,盒模型样式,浮动float,定位position,浏览器默认样式

HTML5 /CSS3

**HTML5:**HTML5 的优势,HTML5 废弃元素,HTML5 新增元素,HTML5 表单相关元素和属性

**CSS3:**CSS3 新增选择器,CSS3 新增属性,新增变形动画属性,3D变形属性,CSS3 的过渡属性,CSS3 的动画属性,CSS3 新增多列属性,CSS3新增单位,弹性盒模型

JavaScript

**JavaScript:**JavaScript基础,JavaScript数据类型,算术运算,强制转换,赋值运算,关系运算,逻辑运算,三元运算,分支循环,switch,while,do-while,for,break,continue,数组,数组方法,二维数组,字符串