function mynew(func, ...args) { const obj = {} obj.__proto__ = func.prototype let result = func.apply(obj, args) return result instanceof Object ? result : obj }
async function request(options, interval, maxCount) { let alreadyRetryCounts = 0 let result const fetchData = async () => { await fetch(options) .then(res => result = res) .catch(() => { alreadyRetryCounts++ if (alredayRetryCounts <= maxCount) { setTimeout(fetchData, interval) } }) } await fetchData() return result }
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');
出了点小问题,没做过await相关的,都是Promise.then,之类的同步写法
script start async1 start async2 // 正确的 promise1 script end async1 end promise2 setTimeout // 我把awiat 语句包括下面那行当成微任务处理了,所以我面试的时候说的是 script start async1 start promise1 script end async2 // 错误的 async1 end promise2 setTimeout
const fib = (n) => { const nums = [0, 1] if (n <= 1) return n for (let i = 2; i <= n; i++) { nums[i] = nums[i - 1] + nums[i - 2] } return nums[n] }
反问:
面试官说我还不错,许愿二面