I’ve been struggling with a question in https://www.codecademy.com/practice/tracks/introduction-to-javascript/modules/asynch-js!
Question:
Write an async
function, matchPromises()
. Your function should have two parameters—both functions that take no arguments and return promises. When invoked, matchPromises()
should invoke the two function arguments and compare the two promises:
- If the promises have the same resolved value,
matchPromises()
should return the string "match"
.
- If the promises have different resolved values,
matchPromises()
should return the string "no match"
.
- If either promise rejects,
matchPromises()
should return the string "error"
.
my solution is:
function firstFunction() {
let number = Math.floor(Math.random() * 10)
return new Promise(function(resolve, reject){
if (number > 5) {
resolve(number)
}
else {
reject(number)
}
})
};
function secondFunction() {
return new Promise(function(resolve){
resolve(Math.floor(Math.random() * 10))
})
};
async function matchPromises(firstFunction,secondFunction) {
let result;
try {
let firstFunctionResult = await firstFunction;
let secondFunctionResult = await secondFunction;
if ( firstFunctionResult === secondFunctionResult) {
result = 'match';
}
else {
result = 'no match'
};
} catch(error) {
result = 'error'
};
console.log(result);
return result;
}
matchPromises(firstFunction(),secondFunction())
I’m probably misreading the question or overcomplicating things, but I can’t get passed this question, even though the console.log()
shows that it’s working as per the spec.
There are some things you need to adjust. First, your last line should not invoke the functions, but simply pass them to matchPromises
, like so (notice the removal of the paren pairs after each function name):
matchPromises(firstFunction, secondFunction)
Next, within matchPromises
, you want to await
a function call, not a function reference, so you should be calling your two functions (notice the appended parens to both lines):
let firstFunctionResult = await firstFunction();
let secondFunctionResult = await secondFunction();
Try those changes and see what happens.
1 Like
function firstFunction(){};
function secondFunction(){};
async function matchPromises(firstFunction,secondFunction) {
let result;
try {
let firstFunctionResult = await firstFunction();
let secondFunctionResult = await secondFunction();
if ( firstFunctionResult === secondFunctionResult) {
result = 'match';
}
else {
result = 'no match'
};
} catch(error) {
result = 'error'
}
};
console.log(result)
This code block return the right strings.
async function matchPromises(func1, func2) {
func1 = new Promise((resolve, reject) => {resolve(4)});
func2 = new Promise((resolve, reject) => {resolve(4)});
await Promise.all([func1, func2]).then((values) => {
if (values[0]===values[1]) {
return "match";
};
if (values[0]!==values[1]) {
return "no match";
};
})
.catch((error) => {
return "error";
});
}
matchPromises();
func1 = new Promise((resolve, reject) => {resolve(4)});
func2 = new Promise((resolve, reject) => {resolve(4)});
// return "match"
func1 = new Promise((resolve, reject) => {resolve(5)});
func2 = new Promise((resolve, reject) => {resolve(4)});
// return "no match"
func1 = new Promise((resolve, reject) => {reject(4)});
func2 = new Promise((resolve, reject) => {resolve(4)});
// return "error"
But - the check in the quiz still fail.
Maybe someone can assist with what is wrong.
Hi again. Based on chuckwondo’s reply I rewrote the code.
This works!
func1 = () => {
return new Promise((resolve, reject) => {resolve(4)}); // try for example with resolve(5) with the result "no match" and reject(4) with the result "error"
};
func2 = () => {
return new Promise((resolve, reject) => {resolve(4)}); //
};
async function matchPromises(func1, func2) {
try {
let func1Result = await func1();
let func2Result = await func2();
if (func1Result===func2Result) {
console.log("match");
return "match";
} else {
console.log("no match");
return "no match";
};
} catch(err) {
console.log("error");
return "error";
};
}
matchPromises(func1, func2);