How to block on asynchronous functions in JavaScript

前端 未结 5 1771
臣服心动
臣服心动 2020-12-09 12:20

I need to write a function in JavaScript, which returns a state from calling an asynchronous function. However, the caller only receives the value and no callback function i

5条回答
  •  余生分开走
    2020-12-09 12:52

    I think you're looking for StratifiedJS, http://stratifiedjs.org It allows you to "orchestrate" your async code exactly like you wrote, BEWARE that it "writes" like synchronous code, it will NOT block the rest of your application. You can use it anywhere by loading the apollo js library.

    This is how it would look like in Stratified JavaScript:

    function getState() {
      waitfor (var ret) {
        // block on the asynchronous call
        asyncCall("request", resume);
      }
      return ret;
    }
    

    of course there are modules/libraries that would just make it look like this: http://onilabs.com/modules#http

    function getState() {
      return http.get("request");
    }
    

提交回复
热议问题