【问题标题】:Wait for Promise to be fulfilled (Javascript)等待 Promise 完成(Javascript)
【发布时间】:2018-03-15 18:53:03
【问题描述】:

如何暂停他的 Javascript 函数,直到 Promise(在这种情况下为 OAuth2)被满足,以便我可以获得它应该返回的重定向 URL?

在 Firefox Webextension 中使用常规 JS,运行最新的 Firefox Developer (54.0a2)

function fetchOAuth2()
{
    var redirectURL = browser.identity.getRedirectURL() + "page.html";
    console.log(redirectURL);
    var clientID = "CLIENT ID";  
    var authURL = "https://trakt.tv/oauth/authorize";  // API I'm using
    authURL += `?response_type=code`;
    authURL += `&client_id=${clientID}`;
    authURL += `&redirect_uri=${redirectURL}`;
    console.log(authURL);
    var authorizationCode = browser.identity.launchWebAuthFlow({
        interactive: true,
        url: authURL
    });
}

【问题讨论】:

    标签: javascript api oauth-2.0 promise


    【解决方案1】:

    有多种有效的方法,因此请选择您最喜欢/最适合您的代码的方法。

    使用 promise.then()

    function fetchOAuth2()
    {
        var redirectURL = browser.identity.getRedirectURL() + "page.html";
        console.log(redirectURL);
        var clientID = "CLIENT ID";  
        var authURL = "https://trakt.tv/oauth/authorize";  // API I'm using
        authURL += `?response_type=code`;
        authURL += `&client_id=${clientID}`;
        authURL += `&redirect_uri=${redirectURL}`;
        console.log(authURL);
    
        browser.identity.launchWebAuthFlow({
            interactive: true,
            url: authURL
        })
        .then(function(authResult) {
            // do something with authResult
        })
    }
    

    如果您想在此函数之外访问 authResult,则需要从调用站点返回承诺,并 then 它,即

    function fetchOAuth2()
    {
        var redirectURL = browser.identity.getRedirectURL() + "page.html";
        console.log(redirectURL);
        var clientID = "CLIENT ID";  
        var authURL = "https://trakt.tv/oauth/authorize";  // API I'm using
        authURL += `?response_type=code`;
        authURL += `&client_id=${clientID}`;
        authURL += `&redirect_uri=${redirectURL}`;
        console.log(authURL);
    
        return browser.identity.launchWebAuthFlow({
            interactive: true,
            url: authURL
        })
    }
    
    fetchOAuth2().then(function(authResult) {
        // do something with auth result
    });
    

    使用 ES7 异步/等待

    这是一个相对较新的语言功能,但在 FF54 中受支持。如果您需要支持其他浏览器,则必须使用 Babel 来实现兼容性。

    async function fetchOAuth2()
    {
        var redirectURL = browser.identity.getRedirectURL() + "page.html";
        console.log(redirectURL);
        var clientID = "CLIENT ID";  
        var authURL = "https://trakt.tv/oauth/authorize";  // API I'm using
        authURL += `?response_type=code`;
        authURL += `&client_id=${clientID}`;
        authURL += `&redirect_uri=${redirectURL}`;
        console.log(authURL);
    
        var authResult = await browser.identity.launchWebAuthFlow({
            interactive: true,
            url: authURL
        });
    
        // do something with authResult
    }
    

    【讨论】:

    • 非常感谢您的快速回复!感谢您提供多种选择+信息,总是很高兴知道!可悲的是,它似乎没有奏效。可能是一个网络扩展的东西(因为 browser.identity.launchWebAuthFlow() 打开一个新窗口),但由于某种原因,这些解决方案都不起作用。我总是在 foo.then (console.log("test")) 中的 await / 之后进行测试,但是没有控制台输出。有什么想法吗?
    • 检查控制台中的错误,但使用 Ctrl+J 打开 浏览器 控制台,因此您也会看到来自扩展的错误。
    • @trincot 我得到的只是:btgallery.js:1:52329 中的 return 语句后无法访问的代码(据我所知,这不是我的) ReferenceError: event is not defined pop.js:4: 6264(据我所知,也不是我的)没有我自己的应用程序,可悲的是
    • @trincot 道歉,在调试控制台上,而不是浏览器控制台上。在此,我发现了一个错误,说:{RedirectURL} 使用了无效的安全证书。此证书仅对以下名称有效:*.allizom.org, allizom.org SSL_ERROR_BAD_CERT_DOMAIN"(从母语翻译,如有错误请见谅)
    • 格拉格格丹。希望您找到该错误的解决方案。
    猜你喜欢
    • 2015-08-15
    • 1970-01-01
    • 2016-12-18
    • 1970-01-01
    • 1970-01-01
    • 2021-10-08
    • 2019-03-31
    • 2018-07-17
    • 1970-01-01
    相关资源
    最近更新 更多