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

Solution/Concepción Orellana #24

Open
wants to merge 4 commits 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
50 changes: 43 additions & 7 deletions 1-callbacks/solution.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,52 @@ node solution.js name1 name2 name3
** give a look to node.js util.promisify, avoid to alter the validate-user.file **
*/

function solution() {
// YOUR SOLUTION GOES HERE

// you get your 5 names here
const validateUser = require('./validate-user');

let success = [];
let failure = [];
// you get your 5 names here
const names = [
'John',
'Barlowe',
'Caddel',
'Hart',
'Katz',
'Laurier',
'Mary',
];

function solution(addUser) {
// YOUR SOLUTION GOES HERE
// iterate the names array and validate them with the method
for (let index = 0; index < names.length; index++) {
const element = names[index];

validateUser(element, (error, person) => {
// log the final result
if (typeof person === 'undefined') {
failure.push(error.message)
} else {
addUser(person, index)
}
});
}
}

// iterate the names array and validate them with the method
function add(person, index) {
success.push(person)

// log the final result
if (index == (names.length - 1)) {
printResult();
}
}

solution()
function printResult() {
console.log("Success");
success.forEach(element => console.log(`ID: ${element.id}, Name: ${element.name}`));

console.log("Failure");
failure.forEach(element => console.log(element));
}

solution(add);
42 changes: 32 additions & 10 deletions 2-promises/solution.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,38 @@ const id = yourRandomMethod() //third run
7. log the resultant fullname, or the error, at the end
*/

function solution() {
// YOUR SOLUTION GOES HERE

// You generate your id value here

// You call the lastnames method with your id
const getFirstName = require('./firstnames');
const getLastName = require('./lastnames');

// You call the firstname method

// You log the fullname, or error, here
function solution() {
// YOUR SOLUTION GOES HERE
let fullName = { firstName: null, lastName: null };
// You generate your id value here
function getRandomId() {
if (Math.random() < 0.5) {
return null;
} else {
return Math.floor(Math.random() * 201) - 100;
}
}

// You call the lastnames method with your id
getLastName(getRandomId())
.then((lastName) => {
fullName.lastName = lastName;
return getFirstName(lastName);
})

// You call the firstname method
.then((firstName) => {
fullName.firstName = firstName;

// You log the fullname, or error, here
console.log(`fullName: ${fullName.lastName}, ${fullName.firstName}`);
})
.catch((error) => {
console.log(error.message);
});
}

solution()
solution();
93 changes: 86 additions & 7 deletions 3-async-await/solution.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,95 @@ Example:
9. as extra challenge: add Promise.race() and Promise.any(), and try to get the idea of what happens
*/

function solution() {
// YOUR SOLUTION GOES HERE
const getProduct = require('./products');
const getPrice = require('./prices');

// You generate your id value here
async function solution() {
// YOUR SOLUTION GOES HERE
let text = { id, product: null, price: null };

// You use Promise.all() here
// You generate your id value here
let id = Date.now().toString().slice(-2);

// You use Promise.allSettled() here
// You use Promise.all() here
console.log('----segment of Promise.all():');
try {
await Promise.all([
(async () => {
const product = await getProduct(+id);
text.product = product;
})(),
(async () => {
const price = await getPrice(+id);
text.price = price;
})(),
]);

// Log the results, or errors, here
console.log(text);
} catch (error) {
console.log(`without data for the id: ${id}`);
}

// You use Promise.allSettled() here
console.log('\n----segment of Promise.allSettled():');
const promiseSettled = await Promise.allSettled([
getPrice(+id),
getProduct(+id),
]);

let result = promiseSettled.map((v) => {
if (v.status === 'fulfilled') {
return v.value;
}

return 'not found';
});

text.price = result[0];
text.product = result[1];

// Log the results, or errors, here
console.log(text);

// You use Promise.any() here
console.log('\n----segment of Promise.any():');

try {
const promiseRaced = await Promise.any([getPrice(+id), getProduct(+id)]);

if (!isNaN(Number(promiseRaced))) {
text.price = promiseRaced;
delete text.product;
} else {
text.product = promiseRaced;
delete text.price;
}

// Log the results, or errors, here
console.log(text);
} catch (error) {
console.log(`without data for the id: ${id}`);
}

// You use Promise.race() here
console.log('\n----segment of Promise.race():');

try {
const promiseRaced = await Promise.race([getPrice(+id), getProduct(+id)]);

if (!isNaN(Number(promiseRaced))) {
text.price = promiseRaced;
delete text.product;
} else {
text.product = promiseRaced;
delete text.price;
}

// Log the results, or errors, here
console.log(text);
} catch (error) {
console.log(`without data for the id: ${id}`);
}
}

solution()
solution();