I just started working with Promise objects in JS and I can't figure out something.
I need to create a function that receives a Promise object as parameter, if the promise is resolved I need to return a success object, otherwise an empty Error object. But it seems to not be returning anything.
export default function handleResponseFromAPI(promise) {
promise.then(() => {
console.log('Got a response from the API');
return { status: 200, body: 'Success' };
}, () => {
console.log('Got a response from the API');
return new Error();
});
}
And I call it here:
import handleResponseFromAPI from "./2-then";
const promise = Promise.resolve();
const returnObject = handleResponseFromAPI(promise);
console.log(returnObject);
What shows up on console:
undefined
Got a response from the API
I can see that first it returns undefined and then prints the message from my function. Anyone cares to explain the process of this?