babaloo
babaloo

Reputation: 13

shopify-api-node: promise not returning value

Setting up a node.js app to retrieve order(s) information from shopify. I'm trying to store that data and run some logic, but I can't seem to get a hold of it.


THIS WORKS:

shopify.order.list()
.then(function(orders){
    console.log(orders);
  });

THIS DOES NOT WORK:

var orders_json;
shopify.order.list()
    .then(function(orders){
        orders_json=orders;
          //console.log(orders);
      });

console.log(orders_json); //returns undefined

Upvotes: 1

Views: 931

Answers (1)

Max Baldwin
Max Baldwin

Reputation: 3472

Let me introduce you to the world of async/await. As long as you declare your function as async and the function you are "awaiting" returns a promise, you can handle this in a more synchronous way. Have a look at the docs linked above. Notice how I called the async function after it was declared. You can't call await outside the scope of an async function.

async function fetchOrders() {
  try {
    const orders_json = await shopify.order.list();
    // do stuff with orders_json

    return orders_json;
  } catch(err) {
    // handle err
  }
}

const orders = fetchOrders();

Upvotes: 2

Related Questions