0

I'm really confused about why I can not return the JSON result from amazonMws.products.search() and could use some help understanding what is going on. When I write it this way gives me undefined:

function listMatchingProducts(query) {
    const options = {
        Version: VERSION,
        Action: 'ListMatchingProducts',
        MarketplaceId: MARKET_PLACE_ID,
        SellerId: SELLER_ID,
        Query: query
    }

    amazonMws.products.search(options, (err, res) => {
        if(err){
            throw(err)
            return
        }

        return res
    })
}

I also get undefined when using amazonMws.products.search().then().catch() as well.

If I return amazonMws.products.search() I get a promise back instead of the result.

Inside of the callbacks if I console.log(res) I get back the JSON result I'm expecting. So this led me to believe I need to use async await I think, but this results in Promise { <pending> }:

async function listMatchingProducts(query) {
    const options = {
        Version: VERSION,
        Action: 'ListMatchingProducts',
        MarketplaceId: MARKET_PLACE_ID,
        SellerId: SELLER_ID,
        Query: query
    }

    return await amazonMws.products.search(options)
    .then(res => {
        return res
    })
    .catch(e => errorHandler(e))
}

I am totally lost, so if someone could explain to me what is going on, that would be greatly appreciated.

Dustin
  • 567
  • 2
  • 5
  • 18
  • 1
    It's asynchronous. You cannot get a value from the future. A promise for it is the best you will get. At the place where you're calling this, wait for it. – Bergi Mar 17 '18 at 22:58
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – str Mar 17 '18 at 23:23

3 Answers3

2

The amazonMws.products.search function is asynchronous, meaning that it will give you a value later, and because of this, you can't get the value now. Instead, you'll have to say what you want to do later when you receive the value.

This is what returning the promise does. The promise itself is the representation of this value that you'll receive later.

function listMatchingProducts(query) {
    const options = {
        Version: VERSION,
        Action: 'ListMatchingProducts',
        MarketplaceId: MARKET_PLACE_ID,
        SellerId: SELLER_ID,
        Query: query
    }

    return amazonMws.products.search(options)
}

Then, when calling the function, attach a handler to the promise.

listMatchingProducts(someQuery)
  .then(result => {/* do something with result */})
  .catch(error => {/* handle the error */})

And, though you don't need to use async await here, it can make the code look a little nicer in some situations, as though it were synchronous. Here's what calling the above function would look like with async await:

async function getProducts() {
  try {
    const result = await listMatchingProducts(someQuery)
    // do something with result
  } catch (error) {
    // handle the error
  }
}

And, as usual, always consult the docs for any detail you're confused about:

kingdaro
  • 11,528
  • 3
  • 34
  • 38
  • In the API it says I can use promises, or callbacks. But when I use `amazonMws.products.search(options, (err, res) => { return res; })` I still get a promise returned. How do I simply return the JSON? – Dustin Mar 17 '18 at 23:22
  • 1
    You can't return the value like you would normally from another function, because the returned value doesn't exist until later. You have to return the promise and use that. – kingdaro Mar 18 '18 at 00:34
1
function listMatchingProducts(query) {
    const options = {
        Version: VERSION,
        Action: 'ListMatchingProducts',
        MarketplaceId: MARKET_PLACE_ID,
        SellerId: SELLER_ID,
        Query: query
    }

    return amazonMws.products.search(options); //returns a promise
}
Artur P.
  • 886
  • 4
  • 12
0

As others have pointed out, Promises just don’t work the way you think they do.

See my answer to function returning too early before filter and reduce finish for a (hopefully) clear explanation of the problem you face.

Rob Raisch
  • 17,040
  • 4
  • 48
  • 58