简体   繁体   English

预期返回箭头中的值 function 警告

[英]Expected to return a value in arrow function warning

this pice of code finds a user by id and if is user is not found it creates one but...这段代码通过 id 找到一个用户,如果没有找到用户,它会创建一个但是......

I´m getting the "Expected to return a value in arrow function array-callback-return" warning我收到“预期返回箭头 function array-callback-return 中的值”警告

findUser(id)
    .then((user) => {
        if (user.empty) {
            return createUser()
                .then((user) => setUser(user));
        } else {
            const updatedUser = user.docs[0];
            return {
                id: updatedUser.id,
                ...updatedUser.data(),
            };
        }
    })

I can´t get rid of it and I´m not sure why is happening.我无法摆脱它,我不确定为什么会这样。 thanks.谢谢。 hope this piece of code helps you don´t want to see it all: is a mess :)希望这段代码能帮助你不想看到这一切:一团糟 :)

The problem is that you're not consistently returning a value from inside findUser 's .then .问题是您没有始终如一地从findUser.then中返回一个值。 You should return the createUser call:您应该返回createUser调用:

if (user.empty) {
  return createUser()
    .then

This is useful because it means that the resulting Promise chain will resolve when createUser and setUser finish.这很有用,因为这意味着生成的 Promise 链将在createUsersetUser完成时解析。 Otherwise, the createUser / setUser Promise will be disconnected from the outside - you won't be able to determine when it finishes, or handle its errors.否则, createUser / setUser Promise 将与外部断开连接——您将无法确定它何时完成或处理它的错误。

Note that you don't need an anonymous callback which calls setUser - you can pass the setUser function itself to the .then :请注意,您不需要调用setUser的匿名回调 - 您可以将setUser function 本身传递给.then

return createUser()
  .then(setUser);

You also might consider using async / await , the control flow will be clearer:你也可以考虑使用async / await ,控制流程会更清晰:

const user = await findUser(id);
if (user.empty) {
  const createdUser = await createUser();
  // no need to await below if setUser doesn't return a Promise
  await setUser(user);
} else {
  const updatedUser = user.docs[0];
  return {
    id: updatedUser.id,
    ...updatedUser.data(),
  };
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 箭头 function 预期返回值 - Arrow function expected a return value 正确警告:“预期在箭头函数数组回调返回中返回一个值” - Correct warning : "Expected to return a value in arrow function array-callback-return " Firebase云功能警告:“箭头功能预期没有返回值一致 - 返回” - Firebase Cloud Functions warning: “Arrow function expected no return value consistent-return” 反应警告 预期在箭头函数数组-回调-返回结束时返回一个值 - react warning Expected to return a value at the end of arrow function array-callback-return 如何修复警告“预计在箭头 function 数组回调返回中返回一个值” - How fix warning “Expected to return a value in arrow function array-callback-return” 为什么我收到此警告 159:35 警告预计将在箭头 function 数组回调返回的末尾返回一个值? - Why i am getting this warning 159:35 warning Expected to return a value at the end of arrow function array-callback-return? 如何修复预期在反应js中的箭头function警告末尾返回值? - How to fix expected to return a value at the end of arrow function warning in react js? 如何修复“预期在箭头函数末尾返回一个值”警告? - How do I fix "Expected to return a value at the end of arrow function" warning? 预期在react中的箭头函数末尾返回一个值 - Expected to return a value at the end of arrow function in react 重构:期望在箭头函数的末尾返回一个值 - Refactor: Expected to return a value at the end of arrow function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM