javascript - How to do asynchronous initialization in a RequireJS module -


i'm trying create module need needs fetch data url before initialization complete. suspect missing obvious how make requirejs wait until async call complete before require call satisifed.

// data module (function() {   papaparse("http://url.csv", {     complete: function(results) {       /* need return these results module after async call */     }   });    /* return here? */ })  // main require(["data"], function(d) {   /* displays "undefined"  becase async call not complete yet */   console.log(d); }) 

there official plugin dedicated task: text

instead of splitting process 2 steps you'd define csv data needed dependency, in spirit of amd/requirejs:

require(["./csvprocessor", "text!./url.csv"], function(csvprocessor, csvdatastring) {   var data = processor.process(csvdatastring); }) 

note module's url still affected xhr limitations, per the documentation, data file need hosted on same domain app running (unless using cors, etc.).


Comments

Popular posts from this blog

How to connect android app to App engine -

gcc - MinGW's ld cannot perform PE operations on non PE output file -

php - display validation error message next to the textbox in codeigniter -