fetch中如何正确编写lambda表达式
How to correctly write lambda expression in fetch
我的 react.js 项目中有简单的提取:
export function getUserById(id) {
const requestOptions = {
method: 'GET',
headers: { 'Content-Type': 'application/json','Access-Control-Allow-Origin':'http://localhost:9000' },
mode: 'no-cors',
};
var res = fetch("http://localhost:9000/user/"+id);//there should be a lot of strange lambdas..
return res;
}
我在其他文件中使用了这个函数:
var user = getUserById(userStr);
//some code
//more code
var email=user.mail;
var id = user.id;
不幸的是 - res
总是 Promise。我感觉不到函数式编程和反应,但我需要这样做。我知道应该有一些 lambda 表达式,但我不知道如何编写它们。如您所见,我需要从返回 JSON 中获取电子邮件和 ID。
提前致谢。
您需要 await
提取调用。
export async function getUserById(id) {
const requestOptions = {
method: 'GET',
headers: { 'Content-Type': 'application/json','Access-Control-Allow-Origin':'http://localhost:9000' },
mode: 'no-cors',
};
var res = fetch("http://localhost:9000/user/"+id);//there should be a lot of strange lambdas..
return res;
}
var user = await getUserById(userStr);
//some code
//more code
var email=user.mail;
var id = user.id;
编辑:您不需要 await
提取调用,您只需要 await getUserById(userStr)
。
答案没那么容易。正确的方法(await returns promise!)有点问题。这是我的解决方案 ;) :
return fetch("http://your.fancy.link").then((response) => response.json())
和
function App() {
const [state, setState] = useContext(MyContext);
const [loading,setLoading] = useState(true);
const readUrlParamsFromAuthentication = () => {
var url_string = window.location.href; //window.location.href
var url = new URL(url_string);
var userStr = url.searchParams.get("user-id");
var userId = null;
var userEmail = null;
if (userStr) {
getUserById(userStr).then(user=>
{
localStorage.setItem('email', user.email);
setState({ email: user.email, isLogged: true });
console.log("TUTAAAAJ");
console.log(user.email,user.id,state.isLoggedIn);
setLoading(false);
}
);
console.log("UserStr");
}
}
useEffect(() => {
readUrlParamsFromAuthentication();
}, [])
there should be your return html ;)
我的 react.js 项目中有简单的提取:
export function getUserById(id) {
const requestOptions = {
method: 'GET',
headers: { 'Content-Type': 'application/json','Access-Control-Allow-Origin':'http://localhost:9000' },
mode: 'no-cors',
};
var res = fetch("http://localhost:9000/user/"+id);//there should be a lot of strange lambdas..
return res;
}
我在其他文件中使用了这个函数:
var user = getUserById(userStr);
//some code
//more code
var email=user.mail;
var id = user.id;
不幸的是 - res
总是 Promise。我感觉不到函数式编程和反应,但我需要这样做。我知道应该有一些 lambda 表达式,但我不知道如何编写它们。如您所见,我需要从返回 JSON 中获取电子邮件和 ID。
提前致谢。
您需要 await
提取调用。
export async function getUserById(id) {
const requestOptions = {
method: 'GET',
headers: { 'Content-Type': 'application/json','Access-Control-Allow-Origin':'http://localhost:9000' },
mode: 'no-cors',
};
var res = fetch("http://localhost:9000/user/"+id);//there should be a lot of strange lambdas..
return res;
}
var user = await getUserById(userStr);
//some code
//more code
var email=user.mail;
var id = user.id;
编辑:您不需要 await
提取调用,您只需要 await getUserById(userStr)
。
答案没那么容易。正确的方法(await returns promise!)有点问题。这是我的解决方案 ;) :
return fetch("http://your.fancy.link").then((response) => response.json())
和
function App() {
const [state, setState] = useContext(MyContext);
const [loading,setLoading] = useState(true);
const readUrlParamsFromAuthentication = () => {
var url_string = window.location.href; //window.location.href
var url = new URL(url_string);
var userStr = url.searchParams.get("user-id");
var userId = null;
var userEmail = null;
if (userStr) {
getUserById(userStr).then(user=>
{
localStorage.setItem('email', user.email);
setState({ email: user.email, isLogged: true });
console.log("TUTAAAAJ");
console.log(user.email,user.id,state.isLoggedIn);
setLoading(false);
}
);
console.log("UserStr");
}
}
useEffect(() => {
readUrlParamsFromAuthentication();
}, [])
there should be your return html ;)