javascript - Wait for function to finish execution -


i have following:

function functiona() {     var myvar = functionb();     functionc(myvar); } 

the time, functionb needs answer, depends on user input. may 1s, 10s. functionc called undefined value, because functionb() hasnt't finished yet. tried this:

function functiona() {     var def = $.deferred();     var myvar = functionb();     def.resolve();     $.when(def).done(function () {         functionc(myvar);     }); } 

this doens't work. saw on stackoverflow: javascript function wait until function finish how can transferred problem? recap, execution of functiona needs stop, until functionb() has answered. thanks.

you can change functionb return deferred object, can resolve within async handler, this:

function functiona() {     var deferred = functionb();     $.when(deferred).done(functionc); }  function functionb() {     var deferred = $.deferred();      // async here...     // asynccallback() {      //     deferred.resolvewith(this, dataretrieved);        // }      return deferred; }  function functionc(data) {     console.log(data); } 

Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -