Skip to content
This repository was archived by the owner on Sep 1, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions problem/implement-basic-debounce_en.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,34 @@
## Overview

There is no solution yet.
Debouncing is a programming practice used to ensure that time-consuming tasks do not fire so often, helping to improve performance and responsiveness. This is particularly useful in scenarios like handling input events, resizing windows, or fetching data from an API.

Would you like to [contribute to the solution](https://github.com/BFEdev/BFE.dev-solutions/blob/main/problem/implement-basic-debounce_en.md)? [Contribute guideline](https://github.com/BFEdev/BFE.dev-solutions#how-to-contribute)
The `debounce` function prevents a function from being called too frequently. It ensures that the function is called only once after a specified wait time has elapsed since the last time the debounced function was invoked.

## Usage

The `debounce` function takes two parameters:

1. `func`: The function to be debounced.
2. `wait`: The number of milliseconds to wait before invoking `func` after the last call.

It returns a new function that, when invoked, will delay the execution of `func` until after `wait` milliseconds have elapsed since the last time the returned function was invoked.

## Implementation

```javascript
/**
* @param {(...args: any[]) => any} func
* @param {number} wait
* @returns {(...args: any[]) => any}
*/
function debounce(func, wait) {
let timeoutId;

return function (...args) {
if (timeoutId) clearTimeout(timeoutId);
timeoutId = setTimeout(() => {
func(...args);
}, wait);
};
}
```
69 changes: 67 additions & 2 deletions problem/implement-promise-all_en.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,69 @@
## Overview

There is no solution yet.
The `all` function is a polyfill for the native `Promise.all` method. It takes an array of promises and returns a new promise that resolves when all the promises in the array have resolved or rejects if any of the promises in the array reject. This custom implementation also handles cases where the input array might contain non-promise values.

Would you like to [contribute to the solution](https://github.com/BFEdev/BFE.dev-solutions/blob/main/problem/implement-promise-all_en.md)? [Contribute guideline](https://github.com/BFEdev/BFE.dev-solutions#how-to-contribute)
## Implementation

Here's the implementation of the `all` function:

```javascript
/**
* @param {Array<any>} promises - Notice the input might have non-Promises
* @return {Promise<any[]>}
*/

function all(promises) {
if (promises.length === 0) {
return Promise.resolve([]);
}

let resultArray = [];
let resolvedCount = 0;
return new Promise((resolve, reject) => {
promises.forEach(async (p, index) => {
try {
const promiseResult = await p;
resultArray[index] = promiseResult;
resolvedCount++;
if (resolvedCount === promises.length) {
resolve(resultArray);
}
} catch (error) {
reject(error);
}
});
});
}
```

## Explanation

1. **Handling Empty Array:**

- The first check in the function is to see if the `promises` array is empty. If it is, the function returns a resolved promise with an empty array: return `Promise.resolve([]);`.
- This is because if there are no promises to wait for, the result should be an empty array.

2. **Variables Initialization:**

- `resultArray`: An array to store the resolved values of the promises.
- `resolvedCount`: A counter to keep track of how many promises have resolved.

3. **Returning a New Promise:**

- The function returns a new promise that will eventually resolve or reject based on the input promises.
- Inside the promise, we use `promises.forEach` to iterate over each promise in the input array.

4. **Handling Each Promise:**

- For each promise, the `await` keyword is used to wait for the promise to resolve.
- The `await` keyword also handles any non-promise value.
- The resolved value is then stored in `resultArray` at the corresponding index.
- The `resolvedCount` is incremented by 1.

5. **Resolving the Result:**

- After incrementing `resolvedCount`, we check if it matches the length of the input `promises` array.
- If all promises have resolved (`resolvedCount === promises.length`), the `resolve` function is called with the `resultArray`.

6. **Error Handling:**
- If any promise rejects, the `catch` block catches the error, and the `reject` function is called to reject the entire promise.