Unable to call functions with Require.js

前端 未结 2 1773
执念已碎
执念已碎 2021-01-26 09:35

i try to write an module for my node.js server with require.js that just returns the object i want to get from an url. But somehow i can\'t return the values i get with my metho

2条回答
  •  臣服心动
    2021-01-26 10:04

    You can't have a function that makes an async call just return something (unless it's a promise).

    You need to have your function take a callback parameter:

    function foo(callback) {
      doSomethingAsync(function(data) {
        // fire callback, which is a function that takes an argument 'data'
        callback(data)
      });
    }
    

    Then you can use it like this:

    foo(function(data) {
      doStuffWith(data);
    });
    

提交回复
热议问题