0
我正在創建一個登錄腳本,該腳本應該將responseData返回給調用類。問題是,我一直不明確的返回結果。如果我控制檯記錄responseData它顯示正常。我注意到的一件事是,未定義有時出現在控制檯中的控制檯日誌responseData之前。使用React Native返回fetch()undefined
module.exports = {
loginUser: function(emailAddress = "", password = "") {
var url= "URL_HERE";
console.log(url);
fetch(url, {method: "GET"})
.then((response) => response.json())
.then((responseData) =>
{
console.log(responseData);
return responseData;
})
.done(() => {
});
},
otherMethod: function() {}
}
這裏的函數是如何調用
var dataLogin = require('../../data/login.js');
var result = dataLogin.loginUser(this.state.formInputEmail, this.state.formInputPassword);
的[我如何返回從一個異步調用的響應?]可能的複製(HTTPS:/ /stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) –
將回調方法傳遞給loginUser方法,並在獲取響應時調用該方法。 –