GT2/Ejectable/node_modules/@react-navigation/native/lib/module/useThenable.js

36 lines
794 B
JavaScript
Raw Normal View History

2021-08-16 00:14:59 +00:00
import * as React from 'react';
export default function useThenable(create) {
const [promise] = React.useState(create);
let initialState = [false, undefined]; // Check if our thenable is synchronous
promise.then(result => {
initialState = [true, result];
});
const [state, setState] = React.useState(initialState);
const [resolved] = state;
React.useEffect(() => {
let cancelled = false;
const resolve = async () => {
let result;
try {
result = await promise;
} finally {
if (!cancelled) {
setState([true, result]);
}
}
};
if (!resolved) {
resolve();
}
return () => {
cancelled = true;
};
}, [promise, resolved]);
return state;
}
//# sourceMappingURL=useThenable.js.map