预期在箭头函数错误的末尾返回一个值

时间:2018-12-28 15:29:28

标签: javascript ecmascript-6

我有此错误预期在箭头函数一致返回的末尾返回一个值。我不确定如何防止这种情况。

我试图在屏幕尺寸小于1060px时停止Swiper轮播

    import Swiper from 'swiper';

    export default function () {
      let articlesGalleryCarousel;

      const doSomething = () => {
        const enableSwiper = () => {
          articlesGalleryCarousel = new Swiper('.js-swiper-container', {
            loop: true,
            slidesPerView: 'auto',
            centeredSlides: true,
            a11y: true,
            keyboardControl: true,
            grabCursor: true,
            pagination: '.swiper-pagination',
            paginationClickable: true,
            navigation: {
              nextEl: '.carousel-button--prev',
              prevEl: '.carousel-button--next',
            },
          });
        };

        const breakpoint = window.matchMedia('(max-width:1060px)');

        const breakpointChecker = () => {
          if (breakpoint.matches === true) {
            if (articlesGalleryCarousel !== undefined) articlesGalleryCarousel.destroy(true, true);
          } else if (breakpoint.matches === false) {
            return enableSwiper();
          }
        };

        breakpoint.addListener(breakpointChecker);

        breakpointChecker();
      };
      return doSomething;
    }

2 个答案:

答案 0 :(得分:2)

这是ESLint consistent-return警告。

  

”此规则要求return语句始终为“永不”   指定值”

在第一个IF语句的情况下,您的函数“ breakpointChecker”没有返回任何内容。

答案 1 :(得分:1)

ESLint is telling you,箭头函数应该始终或永远不返回值。

您有一个分支返回一个值(return enableSwiper();),而一个分支不返回(如果breakpoint.matches为真)。

那么-您希望该函数始终返回值还是从不返回值?