Skip to content
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

solutions JH #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
9 changes: 9 additions & 0 deletions tests/problem1.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@

const p = require('../utils.js');

let negation = async (promise) => {
try {
let result = await promise;
return Promise.reject(result);
} catch(e) {
return Promise.resolve(e);
}
};

describe('problem1', () => {
it('negates resolved promises', (done) => {
negation(p.resolveWith(1).after(10))
Expand Down
42 changes: 40 additions & 2 deletions tests/problem2.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/**
* Napisz funkcję 'attempt', która przymuje jako jedyny argument listę funkcji, z których
* Napisz funkcję 'attempt', która przyjmuje jako jedyny argument listę funkcji, z których
* każda zwraca Promise'a. 'attempt' powinien wywołać pierwszą funkcję i jeśli zwrócony
* Promise zakończył się błędem, wywołać kolejną funkcję z listy, aż któryś Promise zwróci
* poprawny wynik. 'attempt' powinien zwrócić ten wynik i nie wywoływać już dalszych funkcji.
*
* Przykładowo wywołanie:
*

* attempt([
* () => p.rejectWith(1).after(10),
* () => p.resolveWith(5).after(50),
Expand All @@ -24,6 +24,44 @@

const p = require('../utils.js');

// async await
// let attempt = async (functions) => {
// try {
// return await functions[0]();
// } catch(e) {
// if (functions.length === 1) {
// return Promise.reject(e);
// }
//
// return await attempt(functions.slice(1, functions.length));
// }
// };

let attempt = (functions) => {
return functions[0]().catch((e) => {
if (functions.length === 1) {
return Promise.reject(e);
}

return attempt(functions.slice(1, functions.length));
});
};

// let attempt = async (functions) => {
// do {
// try {
// return await functions[0]();
// } catch (e) {
// if (functions.length === 1) {
// return Promise.reject(e);
// }
// }
//
// } while(functions = functions.slice(1, functions.length));
//
// return Promise.reject('THIS SHOULD NEVER HAPPEN');
// };

describe('problem2', () => {
it('properly resolves with just one promise', async () => {
const result = await attempt([
Expand Down
20 changes: 20 additions & 0 deletions tests/problem3.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,26 @@

const p = require('../utils.js');

let first = async (promises) => {
let errors = [];
let finished = promises.length;
return new Promise((resolve, reject) => {
promises.map(async promise => {
try {
let result = await promise;
resolve(result);
} catch(e) {
errors.push(e);
finished--;

if (finished === 0) {
reject(errors);
}
}
});
});
};

describe('problem3', () => {
it('resolves to first resolved value', async () => {
const result = await first([
Expand Down
29 changes: 28 additions & 1 deletion tests/problem4.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,41 @@
const p4 = p.resolveWith(6).after(6) // resolves in 6 sec.
const r1 = p.rejectWith(4).after(4) // rejects in 4 sec.

takeFirst(2, p1, p2, p3, p4, r1).then(console.log) // [p3, p2, p1]
takeFirst(2, p1, p2, p3, p4, r1).then(console.log) // [p3, p2]

takeFirst(2, p1, p4, r1).catch(console.log) // error thrown by r1

*/

const p = require('../utils.js');

let takeFirst = (count, ...promises) => {
count = promises.length < count ? promises.length : count;

let results = [];
return new Promise((resolve, reject) => {
if (!promises.length) {
resolve([]);
}

promises.map(async promise => {
try {
let result = await promise;

if (results.length < count) {
results.push(result);
}

if (results.length === count) {
resolve(results);
}
} catch(e) {
reject(e);
}
});
});
};

describe('problem4', () => {
it('resolves with an array of as many elements as first argument specifies', async () => {
const result = await takeFirst(
Expand Down