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