1

I am trying to implement a shopify api in node and want to call the post requests synchronously since i want to retrieve the product id after posting and store it in an array. I am making use of a library called deasync as a synchronous waiting loop. The script used to work before and suddenly stopped working. When i log my calls without the deasync loop, i see that responses are returned from shopify, however, when I'm running my script with the deasync loop, my identifier stays null, and the deasync loop keeps iterating. I am relatively new to asynchronous requests in node.js. The scope of my identifier variable seems correct. The only thing I can imagine for now is that the callback happens in a parallel node.js universe that is seperate from my while loop. Any help would be much appreciated, thx!

function postProduct(Store, product, variants) {

  var identifier = null;

  var post_data = {
    "product": product
  };


  Shopify.post('/admin/products.json', post_data, function(err, data, headers) {

    identifier = {
      id: data.product.id,
      price: data.product.price,
      variants: variants
    };

  });

  while(identifier === null) {
    require('deasync').runLoopOnce();
  }

  return identifier;
}
9
  • You should not have synchronous I/O of any kind after the first tick. Commented Nov 26, 2015 at 19:31
  • I got the idea from: stackoverflow.com/questions/21819858/… Commented Nov 26, 2015 at 19:40
  • 3
    Yeah, it's just really not a very good idea. Commented Nov 26, 2015 at 19:43
  • @jov: Well, it's just that. An idea. With a proof of concept. Not something that should really be used unless you know exactly what you're doing. Commented Nov 26, 2015 at 19:45
  • ok, thx for pointing that out, but in this case, any idea why the callback never seems to happen? Commented Nov 26, 2015 at 19:48

1 Answer 1

3

All of this deasync solutions can't to be stable, because they are depend on internal engine implementation.

Just try to learn async way. It's really simple.

Simplified example with native Promise:

var products = [
  {Store: {}, product: {}, variant: {}},
  {Store: {}, product: {}, variant: {}},
];

Promise.all(products.map(postProduct)).then(function(identifiers){
  // use identifiers
});

function postProduct(data) {
  return new Promise(function(ok, fail){
    Shopify.post(...., function(){
       //...
       ok(identifier);
    });

  });
}
Sign up to request clarification or add additional context in comments.

9 Comments

its not really a 'workaround'... just a better way of doing it
interesting, will try and work with this. Was looking at the Async(github.com/caolan/async) library earlier, and seems very similar
Promise is native from node v0.12, async was created before Promise become a standard thing. My own way was async -> Q promises -> es6 Promise
i see async.js as a higher level utility belt - it offers a lot more than you get with promises
@Robbie, In most cases, you do not need these waterfalls and cargos. But sure, it's awesome library.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.