Call An Asynchronous Javascript Function Synchronously

前端 未结 8 1474
南方客
南方客 2020-11-22 17:37

First, this is a very specific case of doing it the wrong way on-purpose to retrofit an asynchronous call into a very synchronous codebase that is many thousands of lines lo

8条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-22 17:46

    You can also convert it into callbacks.

    function thirdPartyFoo(callback) {    
      callback("Hello World");    
    }
    
    function foo() {    
      var fooVariable;
    
      thirdPartyFoo(function(data) {
        fooVariable = data;
      });
    
      return fooVariable;
    }
    
    var temp = foo();  
    console.log(temp);
    

提交回复
热议问题