setTimeout(function timeout() { console.log('Таймаут'); }, 0); let p = new Promise(function(resolve, reject) { console.log('Създаване на обещание'); resolve(); }); p.then(function(){ console.log('Обработка на обещанието'); }); console.log('Край на скрипта'); // Усложнен случай // console.log(1); // setTimeout(() => console.log(2)); // Promise.resolve().then(() => console.log(3)); // Promise.resolve().then(() => setTimeout(() => console.log(4))); // Promise.resolve().then(() => console.log(5)); // setTimeout(() => console.log(6));
Frontend
FE разклонено разклонено разклонено разклонено разклонено разклонено ```JavaScript console.log('Timeout'); // 4 }, 0); let p = new Promise(function(resolve, reject) { console.log('Създаване на обещание'); // 1 resolve(); }); p.then(function(){ console.log('Обработка на обещанието'); // 3 }); console.log('Край на скрипта'); // 2 Объркан случай console.log(1); setTimeout(() => console.log(2)); Promise.resolve().then(() => console.log(3)); Promise.resolve().then(() => setTimeout(() => console.log(4))); Promise.resolve().then(() => console.log(5)); setTimeout(() => console.log(6)); console.log(7);
оставете i = 0; функция increment() { i++; console.log(i) } функция debounce(func, wait = 0) {} const debouncedIncrement = debounce(increment, 100); debouncedIncrement(); // i = 0 debouncedIncrement(); // i = 0 debouncedIncrement(); // i = 0 debouncedIncrement(); // i = 0 debouncedIncrement(); // i = 0 debouncedIncrement(); // i = 0 debouncedIncrement(); // i = 1 setTimeout(debouncedIncrement, 150); // i = 2 setTimeout(debouncedIncrement, 200); // i = 2 setTimeout(debouncedIncrement, 350); // i = 3
function createRateLimiter(limit, interval) {} // Тестове const limiter = createRateLimiter(2, 100); let count = 0; const fn = () => { count++ console.log(count) }; limiter(fn); // count = 1 limiter(fn); // count = 2 limiter(fn); // Игнориране (лимит 2/100ms) setTimeout(() => limiter(fn), 150); // count = 3 (минало >100ms)
/** * @param {Array} iterable * @return {Promise<Array>} */ function promiseAll(iterable) {} // Решен пример. const p0 = Promise.resolve(3); const p1 = 42; const p2 = new Promise((resolve, reject) => { setTimeout(() => { resolve('foo'); }, 100); }); promiseAll([p0, p1, p2]).then(console.log); // [3, 42, 'foo']