including-modules
This commit is contained in:
+28
@@ -0,0 +1,28 @@
|
||||
/**
|
||||
* Calls the given 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 = Promise.resolve(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
|
||||
Reference in New Issue
Block a user