How do I unsubscribe from a promise in bluebird which is using reacthooks?

情到浓时终转凉″ 提交于 2020-07-10 10:26:28

问题


This error has been discussed before in many blogs,forums..etc and I'm speculating that the problem is with not having a promise that is cancellable.I'm wondering how to unsubscribe from a a bluebird promise.

useEffect(() => {
    let isFetchable = !!qualifiedURL;

    const fetchData = async () => {
      let fetcher = Promise.resolve();

      if (isFetchable && !data) {
        setLoading(true);

       if (method === 'GET') {
          fetcher = fetch(method, qualifiedURL);
        } else {
          fetcher = fetch(method, qualifiedURL, {
              ...options,
          });
        }

        fetcher.then((result) => {
          setError(null);
          setData(result);
          setHook(result);
        }).catch((err) => {
          setData(null);
          setError(parseError(err));
        }).finally(() => {
          setLoading(false);
        });
      }

      return fetcher;
    };

    fetchData();

    return () => {
      isFetchable = false;
    };
  }, [qualifiedURL]);

Error:

Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function.
in StepAuthorize (created by O365) react_devtools_backend.js:6:7472
r react_devtools_backend.js:6
React 5
_callee$/< useFetch.js:95
finallyHandler bluebird.js:1988
tryCatcher bluebird.js:5370
_settlePromiseFromHandler bluebird.js:3366
_settlePromise bluebird.js:3423
_settlePromise0 bluebird.js:3468
_settlePromises bluebird.js:3548
_drainQueueStep bluebird.js:145
_drainQueue bluebird.js:138
_drainQueues bluebird.js:154
drainQueues bluebird.js:67

来源:https://stackoverflow.com/questions/62764764/how-do-i-unsubscribe-from-a-promise-in-bluebird-which-is-using-reacthooks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!