node.js應用程序需要使用Promise來確保在運行後續命令之前返回一個函數的結果。但是,下面的代碼凍結在getUser(theToken)
的內部,promise函數應該返回它的值。 爲了使getUser(theToken)
promise函數能夠返回它在內部生成的有效值,需要對下面的代碼進行哪些特定的更改?Node.js Promise函數在返回之前執行凍結
app.get('/auth/provider/callback', function(req, res) {
var queryData = url.parse(req.url, true).query;
//This is the end of Step C in the flow
// code was in the GET request, ex: http://localhost:8080/login?code=w6zGJO&state=IuaO63
console.log('Finished Step C of Authorization Code Flow. Welcome back from authserver');
console.log('queryData.code is: ' + queryData.code);
//Now start Step D in the flow
console.log('About to start Step D of Authorization Code Flow. ');
var tokenUrl = oAuthConfig.tokenUrl + '?grant_type=' + oAuthConfig.grantType + '&code='+queryData.code+'&client_id=' + oAuthConfig.clientID + '&redirect_uri=' + oAuthConfig.callbackUrl;
request.post(tokenUrl, function(err, response, body){
if(err){console.log('ERROR with token request.')}
// Step E is the response received as follows:
var data = JSON.parse(body);
console.log('JSON data response is: ' + data);
getUser(data.access_token).then(function(message) {
console.log('in callback, jwtJSON is: ');console.log(message);
res.redirect(somewhere);
});
});
console.log('The POST is finished. Waiting for response.');
console.log('Step D. Token Request Sent To AuthServer.');
});
然後下面在同一文件中的getUser(theToken)
功能是:
function getUser(theToken) {//returns JWT
return new Promise(function(resolve, reject) {
console.log('in getUser, theToken is: ');console.log(theToken);
var jwtJSON = { "token" : "anonymous" };
request({
method: 'GET', url: authServer + '/uaa/user', json: true,
auth: { user: null, password: null, sendImmediately: true, bearer: theToken }
}, function (error, response, body) {
if(error){
console.log('in getUser, ERROR with user request.');console.log(error);
jwtJSON = { "token" : "error" };
return jwtJSON;//
}
else {
var uname = '';var jwtUser = 'empty';
console.log('in getUser, response.statusCode is: ');console.log(response.statusCode);
if(body['name'] && body['name'].length > 0){
uname = body['name'];console.log('uname is: ');console.log(uname);
client.exists(uname, function(err, reply) {//Check to see if a Redis key for the user already exists
if (reply === 1) {//a redis key DOES exist
console.log('\"'+uname+'\" exists');
client.expire(uname, 10);//set the key to expire in 10 seconds. use this to manage session length
client.hgetall(uname, function(err, object) {
if(object && object["jwt"]) {
jwtJSON = { "token" : object["jwt"] };
console.log('in getUser, jwtJSON is: ');console.log(jwtJSON);
return jwtJSON;
} else { return jwtJSON; }
});
} else {//a redis key DOES NOT exist
console.log('\"'+uname+'\" doesn\'t exist');
jwtUser = generateJwt(uname, authoritiesLocal);
client.hmset(uname, {//store a hash/object
'jwt' : jwtUser
});
jwtJSON = { "token" : jwtUser };console.log('in getUser, jwtJSON is: ');console.log(jwtJSON);
client.expire(uname, 10);//set the key to expire in 10 seconds. use this to manage session length
return jwtJSON;
}
});//end of Redis conditional block
console.log('jwtJSON is: ');console.log(jwtJSON);
} else { return jwtJSON; }
};
});
});
};
控制檯打印輸出是:
Finished Step C of Authorization Code Flow. Welcome back from authserver
queryData.code is: 0oxzu6
About to start Step D of Authorization Code Flow.
The POST is finished. Waiting for response.
Step D. Token Request Sent To AuthServer.
JSON data response is: [object Object]
in getUser, theToken is:
eyJ_SomeLongToken_hbG
in getUser, response.statusCode is:
200
uname is:
user
jwtJSON is:
{ token: 'anonymous' }
"user" doesn't exist
in getUser, jwtJSON is:
{ token: 'eyJ_TheSameLongToken_hbG' }
然後程序執行的序列之前停止幾秒鐘第二次運行,第二次產生錯誤,因爲沒有變量保持第二次填充。
回調函數你真的應該花約1小時閱讀如何組織的承諾,它們是如何工作的數據。這將爲您節省數週的頭痛。這裏有一個很好的開始,讓你的代碼不老雞蛋的氣味:http://solutionoptimist.com/2013/12/27/javascript-promise-chains-2/ –
@JasonSebring謝謝。我會仔細閱讀你的鏈接。在創建這篇文章之前,我也花了幾個小時閱讀類似的鏈接。閱讀至關重要。但是我們很多人都需要對代碼示例進行交互式討論,才能真正瞭解API的工作原理。 – FirstOfMany