Products.All() Promise Pending

products
javascript

#1

Hello,

New to the framework, just trying a simple API call, within react, to a test product i created. However, when i use Moltin.Products.All() i get a return value of Promise pending

Here’s some code:
Index file that holds all my needed Moltin functions:

import Moltin  from './config_moltin';

export const getAllProducts = () => {
  return Moltin.Products.All();
}

Using the Moltin functions within a Component:

  import * as Moltin from '../../../moltin/index';

  componentDidMount() {
    console.log(Moltin.getAllProducts().then(product => product));
  };
 // Result: Promise <pending>

Any idea on how to not get it to return that pending value? I’ve tried doing a new Promise within my index file for Moltin so that i have control over the resolve but i still get the same result. I also tried with async await but i think my setup was wrong.

My apologies if i didn’t put enough info, let me know if i need to explain more.

Any help would be greatly appreciated
Thanks


#2

Hi @mttmortensen

Instead of

componentDidMount() {
  console.log(Moltin.getAllProducts().then(product => product));
};

Could you try

componentDidMount() {
  Moltin.getAllProducts().then(product => console.log(product))
};

Regards,
Jamie :slight_smile:


#3

Ah @notrab thank you that worked! Any reason on why that is?


#4

Hi @mttmortensen

You’re immediately invoking console.log on the promise.

.then() allows you to act on the promise when it resolves as shown in the updated example or rejects using .catch()

I hope this helps :smile:


#5

That makes sense! Thanks for the help :slight_smile:


#6

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.


#7