如果一个Promise(在这种情况下是OAuth2)已经满了,那么如何暂停他的Javascript函数,以便我可以获得它应该返回的重定向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
});
}
答案 0 :(得分:0)
有不同的方法是有效的,因此请选择您喜欢的方法/最适合您的代码。
使用promise.then()
public string Login (string Username, string Password)
{
try {
// many more code here.....
var mySqlServer = DependencyService.Get<ISqlServer>(DependencyFetchTarget.NewInstance);
bool isOnline = mySqlServer.LoginToSqlServer(ConfigurationData.GetSQLServerIP(),
ConfigurationData.GetTechUserDB(),
ConfigurationData.GetTechUser(),
ConfigurationData.GetTechUserPassword());
if (isOnline) {
// Many more code here
}else{
// Many more code here
}
return "";
}catch(Exception ex){
return "Allg. Fehler:" + ex.Message;
}
} // login
如果要访问此函数之外的authResult,则需要返回promise,并从调用站点返回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
})
}
,即
then
使用ES7 async / await
这是一种相对较新的语言功能,但在FF54中受支持。如果您需要支持其他浏览器,则必须使用Babel才能兼容。
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
});