简体   繁体   English

JavaScript的麻烦理解Windows 8的承诺

[英]javascript trouble understanding windows 8 promises

So I am writing a quick app that involves some oauth2 is what the end result to be is a method which is in charge of the oauth access token flow so it will do the following: 因此,我正在编写一个涉及一些oauth2的快速应用程序,最终结果是负责oauth访问令牌流的方法,因此它将执行以下操作:

  1. check if we have a token if we don't get an access token 如果没有访问令牌,请检查是否有令牌
  2. check if the token is expired if not we should refresh it 检查令牌是否过期(如果未过期),我们应该刷新它
  3. otherwise return the token 否则返回令牌

the pesudo code for this generally looks like this: 伪代码通常如下所示:

if token == null
   token = getAccessToken() //I am going block the app until I get my token kthxbi

if expired(token)
   token = refreshAcessToken() //I am going block the app until I refresh my token kthxbi

return token

this way when I call this method I know I am always being returned a valid token. 这样,当我调用此方法时,我知道我总是会得到一个有效的令牌。 The problem I am running into is that to get an access token or to refresh it we need to use a promise. 我遇到的问题是要获取访问令牌或刷新访问令牌,我们需要使用Promise。 The code for that normally looks like this: 该代码通常如下所示:

getAccessToken() {
    return WinJS.xhr({ type: "post", url: "i am a url rawr", 
                     responseType: "json", data: data}).done(

                     function do stuff(){});
}

I get that I can't just return the access code that I get in the function do stuff as its asynchronous (I can however return that promise). 我得到的,我不能只返回访问代码,我得到的功能do stuff是它的异步(我可以但是返回的承诺)。 This leads my problem. 这导致了我的问题。 I do not want to have to wrap every request I do in some sort of asychnronus statement! 我不想用某种asychnronus语句包装我所做的每个请求! I need the access token to do anything else in the app however I cannot get a definite value from the access token untill the promise has been created. 我需要访问令牌才能在应用程序中执行其他任何操作,但是直到创建了Promise之前,我无法从访问令牌中获得确定的值。 (Surley this would mean that I would need to wrap every function that calls this function in a .then() or .done() statement). (Surley,这意味着我需要将所有调用此函数的函数包装在.then()或.done()语句中)。

can someone shed some light on this. 有人可以阐明这一点吗? My perdicument is that my handleOAuth now looks like this 我的看法是我的handleOAuth现在看起来像这样

this.getToken = function() {
    if (token === null) {

        //test is a function that makes a xhr request to the server
        test().done(
              function complete(result) {
                  token = JSON.parse(result.responseText);
                  lastTokenTime = getTokenExpiry(token['expiry']);
              },
              function error(error) {
                  console.log(JSON.stringify(serializedData));
                  console.log(error.innerText);
              }
        );

       //refresh

     return token; #I am going return null as the promise is not done yet
    }

which is obvoius not great as I can't just return the value of token as its dependent on the promise from test() to get the token value. 这是不好的,因为我不能仅仅返回token的值,因为它依赖于test()的promise来获得token的值。 Idealy I'd like for some way of saying don't return the token until we have a value but this breaks the entire purpose of asychronous statements!. 从概念上讲,我想以某种方式说直到我们有了一个值才返回令牌,但这破坏了异步语句的全部目的! What should I do in this situation. 在这种情况下我该怎么办。

 return token; #I am going return null as the promise is not done yet

No, you should return the promise itself. 不,您应该退还诺言。 That's how promises are meant to work. 这就是承诺的实现方式。 You return the promise, and then code consuming yours can bind additional callbacks to the promise's done/fail callbacks. 您返回承诺,然后使用您的代码的代码可以将其他回调绑定到承诺的完成/失败回调。

It looks like you can change from using WinJS to using a standard XMLHttpRequest. 看起来您可以从使用WinJS更改为使用标准XMLHttpRequest。 Then you can make it synchronous and not need to use a promise. 然后,您可以使其同步,而无需使用Promise。 This will bring you back into the synchronous paradigm. 这将带您回到同步范式。

http://social.msdn.microsoft.com/Forums/windowsapps/en-US/b261daa9-5101-4538-989d-435e1681d64c/synchronous-winjsxhr-request?forum=winappswithhtml5 http://social.msdn.microsoft.com/Forums/windowsapps/zh-CN/b261daa9-5101-4538-989d-435e1681d64c/synchronous-winjsxhr-request?forum=winappswithhtml5

You might look into how better to fit into the asynchronous model as it will provide a better user experience. 您可能会研究如何更好地适应异步模型,因为它将提供更好的用户体验。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM