我是Javascript的初学者,我试图从API获取一些数据,并且代码看起来大致如下:
function getData(uid) {
app.login({ email: userEmail, password: userPassword}).then(function(response){
var user = response.user;
var result = user.read(uid).then(function(){
return "foobar";
});
return result;
});
}
function doStuff(uid) {
var result = getData(uid);
// do stuff on result
console.log(result); //returns promise
}
所以我想访问仅返回Promise对象的结果var result = getData(user, uid);
。什么是获取" foobar"的最佳方式。结果?
答案 0 :(得分:1)
您无法从延期请求中返回结果。你应该做的是使用像这样的回调函数......
function getData(uid, callback) {
app.login({ email: userEmail, password: userPassword}).then(function(response){
var user = response.user;
var result = user.read(uid).then(function(){
return "foobar";
});
callback(result);
});
}
function doStuff(uid) {
getData(uid, function(result) {
// do stuff on result
console.log(result); //returns promise
});
}