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/MayraMacedo #7

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
19 changes: 12 additions & 7 deletions 1-callbacks/solution.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// import * as validateUser from './validate-user.js'
/*
INSTRUCTIONS

Expand Down Expand Up @@ -31,16 +32,20 @@ node solution.js name1 name2 name3
5. another challenge is: after you solve the challenge using callback style, in another file promisify the callback and solve it again
*/

function solution() {
// YOUR SOLUTION GOES HERE
const validate = require('./validate-user')

// you get your 5 names here
const names = ['John', 'Mary', 'Richard', 'Stacy','Mayra']

// iterate the names array and validate them with the method
function solution(error, cb = null) {

// log the final result
if (!error) {
console.log(`Success: ${JSON.stringify(cb)}`)
} else {
console.log(`Failure: ${error.message}`)
}
}

solution()

for (const name of names) {
validate(name,solution)
}

30 changes: 22 additions & 8 deletions 2-promises/solution.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,32 @@ const id = yourRandomMethod() //third run
6. use promise chaining accordingly to manage sequential process and error handling
7. log the resultant full name, or the error at the final
*/
const firstnames = require('./firstnames')
const lastnames = require('./lastnames')

function solution() {
// YOUR SOLUTION GOES HERE

// You generate your id value here
const MIN = 0;
const MAX = 100;

// You call the lastnames method with your id
const random = (min, max) =>{
return Math.floor((Math.random() * (max - min + 1)) + min);
}

// Now, with your recently obtained lastname you call the firstname method
function solution() {

// You log the full name here
// If there's an error, log it
const index = random(MIN,MAX)
lastnames(index)
.then( (firstNameResponse)=>{
firstnames(firstNameResponse)
.then((lastNameResponse)=>{
console.log(lastNameResponse);
})
.catch((error)=>{
console.log(error.message);
})
})
.catch((error)=>{
console.log(error.message);
})
}

solution()
60 changes: 53 additions & 7 deletions 3-async-await/solution.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,63 @@ the format is up to you

8. as an extra challenge add Promise.race() and Promise.any(), and try to get the idea of what happens
*/
const prices = require('./prices')
const products = require('./products')

function solution() {
// YOUR SOLUTION GOES HERE

// You generate your id value here
const idGenerator = () => {
const id = Date.now().toString();
return parseInt(id.slice(id.length - 2));
}

const promiseAll = async (id)=> {
try{
const allPromise = Promise.all([
products(id),
prices(id),
])

const [product,price] = await allPromise;
mulberry417 marked this conversation as resolved.
Show resolved Hide resolved
console.log({id,product,price});
}
catch(error){
console.log(error.message);
}

}

const promiseAllSettled = async (id) => {

const [ product, price ]= await Promise.allSettled([

products(id),
prices(id),
])


if (product.reason) {
console.log(`PRODUCT: ${product.reason.message}`);

}
if (price.reason) {
console.log(`PRICE: ${price.reason.message}`);

}
mulberry417 marked this conversation as resolved.
Show resolved Hide resolved

// You use Promise.all here
if ((product.value && price.value)) {

// You use Promise.allSettled here
console.log({
id,
product:product.value,
price:price.value,
})
}
}
function solution() {

// Log the results or errors here
const id = idGenerator();
promiseAll(id);
promiseAllSettled(id);
}

solution()
solution()