Skip to content

Complited all tasks #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
task5: Rewrite acquisition to promises in closure version
  • Loading branch information
Walik23 committed Jun 12, 2025
commit cf1efcd9aed0b556152faa6d79e896c75e5b19d5
38 changes: 19 additions & 19 deletions JavaScript/4-bad.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,23 @@ const poolify = (factory, size, max) => {

const processQueue = () => {
while (queue.length > 0 && available > 0){
const { callback } = queue.shift();
callback(instances.pop());
const { resolve } = queue.shift();
resolve(instances.pop());
available--;
}
};

const acquire = (callback) => {
if(available > 0){
const instance = instances.pop();
available--;
callback(instance);
}
else {
queue.push({ callback });
}
const acquire = () => {
return new Promise((resolve) => {
if(available > 0){
const instance = instances.pop();
available--;
resolve(instance);
}
else {
queue.push({ resolve });
}
});
};

const release = (instance) => {
Expand All @@ -46,34 +48,32 @@ const size = 2;
const max = 4;
const pool = poolify(createFileBuffer, size, max);

pool.acquire((instance1) => {
(async () => {
const instance1 = await pool.acquire();
console.log('Acquired instance 1', instance1);
setTimeout(() => {
console.log('Releasing instance 1');
pool.release(instance1);
}, 1000);
});

pool.acquire((instance2) => {
const instance2 = await pool.acquire();
console.log('Acquired instance 2', instance2);
setTimeout(() => {
console.log('Releasing instance 2');
pool.release(instance2);
}, 1500);
});

pool.acquire((instance3) => {
const instance3 = await pool.acquire();
console.log('Acquired instance 3', instance3);
setTimeout(() => {
console.log('Releasing instance 3');
pool.release(instance3);
}, 1000);
});

pool.acquire((instance4) => {
const instance4 = await pool.acquire();
console.log('Acquired instance 4', instance4);
setTimeout(() => {
console.log('Releasing instance 4');
pool.release(instance4);
}, 1500);
});
})();
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy