next
Version:
The React Framework
28 lines (27 loc) • 939 B
JavaScript
/**
* Calls the given async function only when the returned promise-like object is
* awaited. Afterwards, it provides the resolved value synchronously as `value`
* property.
*/ export function createLazyResult(fn) {
let pendingResult;
const result = {
then (onfulfilled, onrejected) {
if (!pendingResult) {
pendingResult = fn();
}
pendingResult.then((value)=>{
result.value = value;
}).catch(()=>{
// The externally awaited result will be rejected via `onrejected`. We
// don't need to handle it here. But we do want to avoid an unhandled
// rejection.
});
return pendingResult.then(onfulfilled, onrejected);
}
};
return result;
}
export function isResolvedLazyResult(result) {
return result.hasOwnProperty('value');
}
//# sourceMappingURL=lazy-result.js.map