From 944a00f66f513c47339c12b7d1ad58fe047823fe Mon Sep 17 00:00:00 2001 From: YuanJin <277739025@qq.com> Date: Mon, 1 Nov 2021 11:42:45 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E9=9D=A2=E8=AF=95=E9=A2=98?= =?UTF-8?q?=E6=B1=87=E6=80=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Promise面试题归总.md | 189 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 189 insertions(+) create mode 100644 Promise面试题归总.md diff --git a/Promise面试题归总.md b/Promise面试题归总.md new file mode 100644 index 0000000..7da1332 --- /dev/null +++ b/Promise面试题归总.md @@ -0,0 +1,189 @@ +> Promise的面试题基本都是代码题,不提供参考答案,否则容易干扰思考 +> +> 若需知道正确答案,请自行运行 + + + +1. 下面代码的输出结果是什么 + + ```js + const promise = new Promise((resolve, reject) => { + console.log(1); + resolve(); + console.log(2); + }) + + promise.then(() => { + console.log(3); + }) + + console.log(4); + ``` + +2. 下面代码的输出结果是什么 + + ```js + const promise = new Promise((resolve, reject) => { + console.log(1); + setTimeout(()=>{ + console.log(2) + resolve(); + console.log(3); + }) + }) + + promise.then(() => { + console.log(4); + }) + + console.log(5); + ``` + +3. 下面代码的输出结果是什么 + + ```js + const promise1 = new Promise((resolve, reject) => { + setTimeout(() => { + resolve() + }, 1000) + }) + const promise2 = promise1.catch(() => { + return 2; + }) + + console.log('promise1', promise1) + console.log('promise2', promise2) + + setTimeout(() => { + console.log('promise1', promise1) + console.log('promise2', promise2) + }, 2000) + ``` + +4. 下面代码的输出结果是什么 + + ```js + async function m(){ + const n = await 1; + console.log(n); + } + + m(); + console.log(2); + ``` + +5. 下面代码的输出结果是什么 + + ```js + async function m(){ + const n = await 1; + console.log(n); + } + + (async ()=>{ + await m(); + console.log(2); + })(); + + console.log(3); + ``` + + + +6. 下面代码的输出结果是什么 + + ```js + async function m1(){ + return 1; + } + + async function m2(){ + const n = await m1(); + console.log(n) + return 2; + } + + async function m3(){ + const n = m2(); + console.log(n); + return 3; + } + + m3().then(n=>{ + console.log(n); + }); + + m3(); + + console.log(4); + ``` + +7. 下面代码的输出结果是什么 + + ```js + Promise.resolve(1) + .then(2) + .then(Promise.resolve(3)) + .then(console.log) + ``` + +8. 下面代码的输出结果是什么 + + ```js + var a; + var b = new Promise((resolve, reject) => { + console.log('promise1'); + setTimeout(()=>{ + resolve(); + }, 1000); + }).then(() => { + console.log('promise2'); + }).then(() => { + console.log('promise3'); + }).then(() => { + console.log('promise4'); + }); + + a = new Promise(async (resolve, reject) => { + console.log(a); + await b; + console.log(a); + console.log('after1'); + await a + resolve(true); + console.log('after2'); + }); + + console.log('end'); + ``` + +9. 下面代码的输出结果是什么 + + ```js + async function async1() { + console.log('async1 start'); + await async2(); + console.log('async1 end'); + } + async function async2() { + console.log('async2'); + } + + console.log('script start'); + + setTimeout(function() { + console.log('setTimeout'); + }, 0) + + async1(); + + new Promise(function(resolve) { + console.log('promise1'); + resolve(); + }).then(function() { + console.log('promise2'); + }); + console.log('script end'); + ``` + + \ No newline at end of file