main
Last change
on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago |
Update repo after prototype presentation
|
-
Property mode
set to
100644
|
File size:
1021 bytes
|
Rev | Line | |
---|
[d565449] | 1 | import { useMemo, useRef } from 'react';
|
---|
| 2 | import useEffectOnce from './useEffectOnce';
|
---|
| 3 | var useUnmountPromise = function () {
|
---|
| 4 | var refUnmounted = useRef(false);
|
---|
| 5 | useEffectOnce(function () { return function () {
|
---|
| 6 | refUnmounted.current = true;
|
---|
| 7 | }; });
|
---|
| 8 | var wrapper = useMemo(function () {
|
---|
| 9 | var race = function (promise, onError) {
|
---|
| 10 | var newPromise = new Promise(function (resolve, reject) {
|
---|
| 11 | promise.then(function (result) {
|
---|
| 12 | if (!refUnmounted.current)
|
---|
| 13 | resolve(result);
|
---|
| 14 | }, function (error) {
|
---|
| 15 | if (!refUnmounted.current)
|
---|
| 16 | reject(error);
|
---|
| 17 | else if (onError)
|
---|
| 18 | onError(error);
|
---|
| 19 | else
|
---|
| 20 | console.error('useUnmountPromise', error);
|
---|
| 21 | });
|
---|
| 22 | });
|
---|
| 23 | return newPromise;
|
---|
| 24 | };
|
---|
| 25 | return race;
|
---|
| 26 | }, []);
|
---|
| 27 | return wrapper;
|
---|
| 28 | };
|
---|
| 29 | export default useUnmountPromise;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.