Your start method does not return anything. There is no way to contact the call if it does not return anything. However, your real problem is that you need to wait for the asynchronous action (the user must authorize something in the pop-up window). Thus, you cannot forward calls because call chains require a synchronous (blocking) flow. In other words, there is no way to make your code block until the user answers, and then collect the data synchronously. You must use callbacks.
One of the things I like about JS is the ability to specify inline callbacks, which makes it almost look like the chaining style you're looking for
Here is a suggestion with a simplified version of your code:
function authenticate(callback) { window.oauth_success = function(userInfo) { popupWin.close(); callback(userInfo); } window.oauth_failure = function() { popupWin.close(); callback(null); } var popupWin = window.open('/auth/twitter'); }; } authenticate(function(userInfo){ if (userInfo) { console.log("User succesfully authenticated", userInfo); } else { console.log("User authentication failed"); } });
Juan mendes
source share