-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay78.js
More file actions
21 lines (19 loc) · 795 Bytes
/
Day78.js
File metadata and controls
21 lines (19 loc) · 795 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//* Create a lab to implement time-limited asynchronous functions,
//* which allows you to set a time limit in milliseconds for the given asynchronous function to be executed.
//* If the function takes more time than the specified time limit, it should be rejected with the string "Time Limit Exceeded".
function timeLimitedAsyncFunction(asyncFunction, timeLimit) {
return new Promise((resolve, reject) => {
const timeout = setTimeout(() => {
reject("Time Limit Exceeded");
}, timeLimit);
asyncFunction()
.then((result) => {
clearTimeout(timeout);
resolve(result);
})
.catch((error) => {
clearTimeout(timeout);
reject(error);
});
});
}