resolveWithValue() practice question!

Hello! Can anyone help solve this quiz question? I have been stuck for two days now!

Write a function resolveWithValue() that has two parameters:

  • The first parameter is a function that returns a promise.
  • The second parameter is a string value.

When called, resolveWithValue() should invoke the function argument with the string argument and then log the resultant promise’s resolve value or reject reason to the console.

They provide this code:

let testerFunc = (str) => {
if (Math.random() < .5){
return Promise.resolve(Resolved with: ${str})
} else {
return Promise.reject(Rejected with: ${str})
}
}

Thanks!

Provided link:

https://www.codecademy.com/practice/tracks/introduction-to-javascript/modules/javascript-promises?redirect_url=%2Flearn%2Fintroduction-to-javascript

3 Likes

Hi Deric, Can you find a solution? I have a same problem with this assignaition. Regards,

1 Like

haven’t been able to figure this one out myself.

Here’s my take on the question:

// This is a function returns a promise with a resolved or rejected value based on the string that was passed.
let firstParamFunction = (string) => {
	return new Promise( (resolved, rejected) => {
		if (string === 'yes') {
			resolved('I resolved');
		} else {
			rejected('I was rejected');
		}
	});
};



// This will log the promise using the above function.
// The first parameter is a function that returns a promise.
// The second parameter is a string value.
let resolvedWithValue = (somePromise, string) => {
	console.log(somePromise(string));
}

// Pass the function and a string.
resolvedWithValue(firstParamFunction, 'yes'); // Logs: Promise { 'I resolved' }
resolvedWithValue(firstParamFunction, 'asdsadsa'); // Logs: Promise { <rejected> 'I was rejected' }

Hope that is helpful - I’m having a little trouble understanding promises myself. It’s like the Terminator … The war code of the future is being fought written in the past. Haha.

Hi, are you sure that we have to pass function name ‘firstParamFunction’? If function is defined above, we can pass only the variable ‘str’ to the resolvedWithValue as below (trying to get promises and not sure if it is ok - however it works):

let testerFunc = (str) => {
  if (Math.random() < .5){
    return Promise.resolve(`Resolved with: ${str}`)
  } else {
    return Promise.reject(`Rejected with: ${str}`)
  }
}

const resolveWithValue = (str) => {
  console.log(testerFunc(str));
}

resolveWithValue('sample');
//if (Math.random() < .5) console output is:
//Promise { 'Resolved with: sample' }

New morning, new, better idea:

let testerFunc = (str) => {
    if (Math.random() < .5){
      return Promise.resolve(`Resolved with: ${str}`)
    } else {
      return Promise.reject(`Rejected with: ${str}`)
    }
  }
  
  const resolveWithValue = (str) => {
    testerFunc(str).then((resolved) => {console.log(resolved)}).catch((rejected) => {console.log(rejected)});
  }
  
resolveWithValue('sample');

Nice ideas with your code! I don’t think we have to, as you’ve demonstrated, but the original question was explicitly asking for two parameters.

I was also stuck on this one because of the phrasing. It says the function must take a promise as a parameter along with a string. Am I wrong in thinking that means the promise must be a part of the function declaration? E.g.

const functionName = (promiseParameter, stringParameter) => {
//function body
};

Which means any promise from outside the scope can be an argument in place of promiseParameter?