@grafana/ui
Version:
Grafana Components Library
39 lines (34 loc) • 1.02 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var React = require('react');
;
function useLatestAsyncCall(fn) {
const latestValueCount = React.useRef(0);
const wrappedFn = React.useCallback(
(value) => {
latestValueCount.current++;
const requestCount = latestValueCount.current;
return new Promise((resolve, reject) => {
fn(value).then((result) => {
if (requestCount === latestValueCount.current) {
resolve(result);
} else {
reject(new StaleResultError());
}
}).catch(reject);
});
},
[fn]
);
return wrappedFn;
}
class StaleResultError extends Error {
constructor() {
super("This result is stale and is discarded");
this.name = "StaleResultError";
Object.setPrototypeOf(this, new.target.prototype);
}
}
exports.StaleResultError = StaleResultError;
exports.useLatestAsyncCall = useLatestAsyncCall;
//# sourceMappingURL=useLatestAsyncCall.cjs.map