Timers Promises and AbortController
timers/promises gives awaitable delays. AbortController cancels operations such as timers and fetch.
Example
const { setTimeout: sleep } = require('node:timers/promises');
(async () => {
await sleep(10);
console.log('slept');
const ac = new AbortController();
const p = sleep(1000, null, { signal: ac.signal });
ac.abort();
try { await p; } catch (e) { console.log(e.name); }
})();Result
slept AbortError
Exercise
Cancel an operation.
ac.()
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.