Dustin
Dustin

Reputation: 587

How to return the result of a promise in a function

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.

Upvotes: 0

Views: 65

Answers (3)

Rob Raisch
Rob Raisch

Reputation: 17357

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.

Upvotes: 0

kingdaro
kingdaro

Reputation: 11998

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:

Upvotes: 2

Artur P.
Artur P.

Reputation: 896

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
}

Upvotes: 1

Related Questions