-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path2-abort-problem.js
More file actions
32 lines (26 loc) · 982 Bytes
/
2-abort-problem.js
File metadata and controls
32 lines (26 loc) · 982 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
'use strict';
// Task: implement a cancelable promisify function with AbortController.
// Allow passing new argument `AbortSignal` after the last argument
// to the original function replacing the callback. The promisify should check
// the existence of AbortSignal and apply logic to implement it on top of calling original function
// There is no need to propagate AbortSignal to original function.
// Hint: Create `AbortController` or `AbortSignal` in the usage section.
const promisify = (fn) => (...args) => {
const promise = new Promise((resolve, reject) => {
const callback = (err, data) => {
if (err) reject(err);
else resolve(data);
};
fn(...args, callback);
});
return promise;
};
// Usage
const fs = require('node:fs');
const read = promisify(fs.readFile);
const main = async () => {
const fileName = '1-promisify.js';
const data = await read(fileName, 'utf8');
console.log(`File "${fileName}" size: ${data.length}`);
};
main();