1

Trying to push product id into the array but it's not working! I tried different method using for and for each but no luck at all. it's to simple to not working properly

var preorder = new Array();
      var order_items = new Array();
      var items = req.body.line_items;

      items.forEach(function(data) {
        shopify.product.get(data.product_id)
        .then(function(product){

          console.log(product.id);
          order_items.push(product.id);



        })
        .catch(err => console.error(err));

      });


      console.log(order_items);

Server Log

153161760794
89585614874
[]
Edwin Ca
  • 55
  • 7
  • Possible duplicate of [Asynchronous Process inside a javascript for loop](https://stackoverflow.com/questions/11488014/asynchronous-process-inside-a-javascript-for-loop) – Jared Smith Oct 26 '17 at 19:37
  • 3
    You are logging `order_items` outside of your promise, so no surprise it's empty. You'll need to wait for all those `product.get` promises to resolve before anything will be in `order_items`. – CRice Oct 26 '17 at 19:37
  • 4
    You need to use [Promise.all](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all), and more importantly you need to understand *why* you need to use `Promise.all` – Jared Smith Oct 26 '17 at 19:38

0 Answers0