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.