Given a synchronous function and a time in milliseconds. You need to return a new version of this function, whose execution is limited to the specified time. If the function completes within the given time, the promise resolves with the obtained data. If it does not complete, the promise rejects with the string 'time limit exceeded'.
Frontend
Why might the apply method be needed in runOnce?
/ ** * You need to write an asynchronous function, * which will "sleep" for a specified number of milliseconds, * and then successfully complete * / function sleep(duration) { } // Example const startTime = Date.now(); console.log("start sleeping..."); sleep(2000).then(() => { console.log("Woke up after 2 seconds!"); console.log("Time passed: ", Date.now() - startTime); }); sleep(1000).then(() => { console.log("Woke up after 1 seconds!"); console.log("Time passed: ", Date.now() - startTime); });
Are there problems with adding new methods to built-in object prototypes in patching?
/ ** * Implement the function sumPromises, which takes * promises as arguments and returns the sum * of their resolved results. * * The function can accept any number of arguments. * You can use any Promise APIs. */ // Code here // Example usage const promise1 = Promise.resolve(1); const promise2 = Promise.resolve(2); sumPromises(promise1, promise2).then(console.log); // 3
Implement a runOnce wrapper function that takes a function and returns a new function. The new function can only be called once, and all subsequent calls return undefined. The wrapped function can take arguments and return a result.
/* You need to write a function to calculate the sum of all numeric values in an array. Valid numeric values also include strings that start with digits. In this case, the numeric value should be the initial sequence of digits. The array can contain any data types and may be nested. **You cannot use built-in methods like .flat, .flatMap** */ function sum(arr) { // ... } console.log(sum([1, 'x', '2x', ['3', ['x2', '5']]]));
[name] asked: How long did it take to implement the MVP version of the player?
/** * The function `asyncAuth(callback)` takes a callback, * which can be passed an error (as the first argument) and * data from the backend (as the second argument). * asyncAuth((error, data) => {}); * * You need to implement the `auth()` function, * which calls `asyncAuth()`, but returns a Promise. * * @returns {Promise} */ function auth() { return new Promise((resolve, reject) => { asyncAuth((error, data) => { if (error) { reject(error) } else { resolve(data) } }); }) } /** * The `tryAuth()` function uses `auth()` and, in case of an error, * makes N additional attempts. * If all attempts fail, it should return the last error. * * @returns {Promise} */ function tryAuth(n) { }
/* * Given three code sections. Write the output of each console.log. */ var n = 1; function F(n) { n = 3; } F(n); console.log(n); var obj = { a: 1 }; function F1(o) { o.a = 5; } F1(obj); console.log(obj); var obj = { a: 1 }; function F2(o) { o = { hello: 1 }; } F2(obj); console.log(obj);
What new features from the ES6 standard are used in the code you wrote? What is the arguments object and how does it differ from rest parameters?
How to convert arguments into an array in the old way (ES5), without Array.from and spread operator?
Tell us about the architectural approaches you used on the project? Are you familiar with Feature-Sliced Design (FSD)?
[name] asked: Have you ever written additional jobs in the pipeline yourself?
[name] asked: Have you ever worked on optimizing speed metrics of services?
[name] asked: How was the work on a specific task generally organized: who brought it and groomed it, how was the approval process, what questions arose during implementation, and how did you solve them?
What happens if you do not clear the timeout after the function executes successfully? How to fix it and which promise method is suitable for clearing the timeout in any case (both success and error)?
Implement an asyncAuth function with a callback interface that returns a Promise. Then implement a tryAuth(n) function that uses asyncAuth and makes n additional attempts in case of failure. If all attempts fail, it returns the last error.
/** * The function `asyncAuth(callback)` takes a callback, * which can receive an error (first argument) and data * from the backend (second argument). * asyncAuth((error, data) => {}); * * You need to implement the `auth()` function, * which calls `asyncAuth()`, but returns a Promise. * * @returns {Promise} */ function auth() { // asyncAuth((error, data) => {}); } /** * The function `tryAuth()` uses `auth()` and, in case of an error, * makes N additional attempts. * If all attempts fail - return the last error * * @returns {Promise} */ function tryAuth(n) { }
Implement a function withTimeout that takes a function and a time (milliseconds) and returns a new function. If the original function does not complete within the specified time, the promise should be rejected.