title: Call a callback function inside for of loop
body: <p>I'm making a request with a for of loop to delete all the cart elements, but it doesn't work as expected. Loop works for first iteration but API returns an error in the second iteration.</p>
  <p>I think it's a logical error, what's the problem with this code?</p>
  <p>My try:</p>
  <pre><code>module.exports.removeAllCartItems = (token) =&gt; {
      this.getCart(token, (error, data) =&gt; {
          if(error){
              Sentry.captureException(error);
          }
  
          const cartItems = data.items
          console.log('------cart items----------')
          console.log(cartItems)
          console.log('------cart items----------')
          for(item of cartItems){
              let productId = item.productId
              let variantId = item.variant.product_id
              console.log('--------item one by one---------')
              console.log(item)
              console.log('--------item one by one---------')
             
              this.removeItem(token, productId, variantId, (error, data)=&gt; {
          
                  if(error) {
                      Sentry.captureException(error);             
                  }
                  console.log('------data-------')
                  console.log(data)
                  console.log('******data*******')
              } )
              
          }
  
          
      })
  }
  </code></pre>
  <p>Remove item method:</p>
  <pre><code>module.exports.removeItem = (token, productId, variantId, callback) =&gt; {
      const url = urlBase + &quot;/removeItem&quot; + `?secretKey=${secretKey}`;
      request(
          {
              url: url,
              method: &quot;DELETE&quot;,
              json: true,
              headers: {
                  &quot;content-type&quot;: &quot;application/json&quot;,
                  Authorization: `Bearer ${token}`,
              },
              body: {
                  secretKey: `${secretKey}`,
                  productId: productId,
                  variantId: variantId,
              },
          },
          (error, response) =&gt; {
              if (error) {
                  Sentry.captureException(error);
                  callback(errMessage, undefined);
              } else {
                  const data = response.body;
                  callback(undefined, data);
              }
          }
      );
  };
  </code></pre>
  <p>Console output:</p>
  <pre><code>------cart items----------
  [
    {
      variant: {
        variation_values: [Object],
        price: 475,
        product_id: '883360541280',
        orderable: true
      },
      _id: '62403a55ec05ea0024e9d965',
      productId: '21736758',
      quantity: 1
    },
    {
      variant: {
        variation_values: [Object],
        price: 58.99,
        product_id: '701643411498',
        orderable: true
      },
      _id: '62403b7eec05ea0024e9d966',
      productId: '25589208',
      quantity: 1
      variation_values: { color: 'P2V', size: '42' },
      price: 475,
      product_id: '883360541280',
      orderable: true
    },
    _id: '62403a55ec05ea0024e9d965',
    productId: '21736758',
    quantity: 1
  }
  --------item one by one---------
  --------item one by one---------
  {
    variant: {
      variation_values: { color: 'JJ3WCXX', size: '9MD' },
      price: 58.99,
      product_id: '701643411498',
      orderable: true
    },
    _id: '62403b7eec05ea0024e9d966',
    productId: '25589208',
    quantity: 1
  }
  --------item one by one---------
  ------data-------
  {
    _id: '6240398eec05ea0024e9d962',
    secretKey: '&lt;secretKey&gt;',
    userId: '623dfb884083b000243cc4a7',
    items: [
      {
        variant: [Object],
        _id: '62403a55ec05ea0024e9d965',
        productId: '21736758',
        quantity: 1
      }
    ],
    __v: 6
  }
  ******data*******
  ------data-------
  &lt;!DOCTYPE html&gt;
  &lt;html lang=&quot;en&quot;&gt;
  &lt;head&gt;
  &lt;meta charset=&quot;utf-8&quot;&gt;
  &lt;title&gt;Error&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;pre&gt;Internal Server Error&lt;/pre&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  
  ******data*******
  </code></pre>
  <p>Why API returns internal server error in the second iteration, is it about my code or something else, any idea ?</p>
  <p>I make it work with that way but still dont know why first implementation doesn't work</p>
  <pre><code>module.exports.removeAllCartItems = (token) =&gt; {
      this.getCart(token, async (error, data) =&gt; {
          if (error) {
              Sentry.captureException(error);
          }
  
          const cartItems = data.items;
  
          for (item of cartItems) {
              let productId = item.productId;
              let variantId = item.variant.product_id;
  
              await new Promise((resolve, reject) =&gt; {
                  this.removeItem(token, productId, variantId, (error, data) =&gt; {
                      if (error) {
                          reject(error);
                          Sentry.captureException(error);
                      }
                      resolve(data);
                  });
              });
          }
      });
  };
  </code></pre>