From 0ab449fb15fe468661a67e32d10b97821b7e8f69 Mon Sep 17 00:00:00 2001 From: YuanJin <277739025@qq.com> Date: Wed, 7 Feb 2024 13:42:29 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=97=A0=E7=AD=94=E6=A1=88?= =?UTF-8?q?=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Promise面试题归总(无答案版).md | 179 +++++++++++++++++++++++++++++++ 1 file changed, 179 insertions(+) create mode 100644 Promise面试题归总(无答案版).md diff --git a/Promise面试题归总(无答案版).md b/Promise面试题归总(无答案版).md new file mode 100644 index 0000000..f07d09c --- /dev/null +++ b/Promise面试题归总(无答案版).md @@ -0,0 +1,179 @@ +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'); + ```