I'm trying to access a piece of data through an API and my company gives me a function called findItemById(id)
that returns a JavaScript promise object. I'm also given a function called loadPromise("item", findItemById())
that takes a key value in the state object (we're using React.js) and a function and assigns that value to that key in state. I'm trying to use findItemById(id)
to get the data I need, but I don't want to keep it in state. When I do this:
findItemById(id).then(function(result) {
console.log(result);
}, function(err) {
console.log(err);
});
I get the exact object I'm looking for in the console. But when I do:
const ObjectIWant = findItemById(id).then(function(result) {
return result;
}, function(err) {
return err;
});
console.log(ObjectIWant);
I get a JavaScript promise object back. If I drill down into the promise in the chrome develper tools, I can see the data I want in there. How can I return the data I'm looking for instead of a promise object?