0

I have an asynchronous piece of code that returns an object. In this case, the createprimatives callback.

var url = "http://example.com/example.ttl";
createprimatives(url, function(dummy) {
  console.log(dummy);
});

I'd like to feed that object to another function.

I tried implementing the top suggestion in: How to return value from an asynchronous callback function? by rewriting the code with a callback calling another:

var url = "http://example.com/example.ttl";
loader(url, function(graph) {
  var height = 800;
  var width = 800;
  console.log(graph);
  draw(width,height,graph);
});

function loader(url, fn) {
  createprimatives(url, function(dummy) {
    console.log(dummy);
    fn(dummy);
  });
};

Either console.log(dummy) or console.log(graph) will return the same object as the simple code in the first snippet in an asynchronous manner.

However, draw fails with a message similar to 'TypeError: "x" is not a function' [1] .
draw calls code for a Force-directed D3 graph, so it is more like : "TypeError: svgContainer.append(...).attr(...).selectAll(...).data(...).enter is not a function" .

i.e.:

The object generated by a console.log when fed into

draw(height,width,graph);

works just fine if the object is assigned to the variable graph first (i.e hardcoding the result of the first code snippet in the loader function) .

var graph = pasted console.log(graph) output into code

However I get a TypeError if I wait for the XMLHTTPRequest embedded in createprimatives and do not initially assign to graph. I think I need a piece of code that waits for the XMLHTTPRequest and create primatives, and then calls draw while passing the object generated from the code in the createprimatives function.

[1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Not_a_function

Community
  • 1
  • 1
  • You might be making this more complicated than it is. `createprimatives` calls your callback with some object. You pass that object to `draw`. `draw` complains that the object doesn't have the right shape. So what is `graph` and how is it wrong? We probably need to see the source of `createprimatives` and `draw` to tell you what the problem is, but it sounds like you're most of the way there yourself. – Rob Lourens Feb 08 '17 at 01:52
  • Yes Rob, the problem is in createprimatives. My code gave D3 a string, when it should be an object. Line 62: https://github.com/bshambaugh/node-arc-d3/blob/master/js/createprimatives.js Resolved. – bshambaugh Feb 09 '17 at 23:28

0 Answers0