0

I am trying to make a request to an external resource in a webpack module (my_request.js), but when I call the method from my main.js script, it returns undefined. Anyone experienced this problem or have any thoughts?

// my_request.js
define(['jquery'], function($) {
    var makeRequest = function() {
        $.get('http://api.example.com', function(data) {
            return data;
        });
    };
});

// main.js
require(['./js/my_request'], function(request) {
    var r = request.makeRequest();
    console.log(r); // returns undefined
});
sammy88888888
  • 458
  • 1
  • 5
  • 18
  • The response returns within `makeRequest`, I can see this if I log to the console before the return statement. The problem seems to be exporting the return value to `main.js`. – sammy88888888 Nov 04 '16 at 19:38
  • Yes, that inner callback function does return the value, but so what? It doesn't return the outer `makeRequest` function, which has already returned before the callback function was even called. – Alexander O'Mara Nov 04 '16 at 19:41
  • Ah ok gotcha, thanks. As you might be able to tell I'm not a js developer :) – sammy88888888 Nov 04 '16 at 19:46

0 Answers0