@aws-amplify/core
Version:
Core category of aws-amplify
35 lines (33 loc) • 1.06 kB
JavaScript
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
Object.defineProperty(exports, "__esModule", { value: true });
exports.deDupeAsyncFunction = void 0;
/**
* returns in-flight promise if there is one
*
* @param asyncFunction - asyncFunction to be deduped.
* @returns - the return type of the callback
*/
const deDupeAsyncFunction = (asyncFunction) => {
let inflightPromise;
return async (...args) => {
if (inflightPromise)
return inflightPromise;
inflightPromise = new Promise((resolve, reject) => {
asyncFunction(...args)
.then(result => {
resolve(result);
})
.catch(error => {
reject(error);
})
.finally(() => {
inflightPromise = undefined;
});
});
return inflightPromise;
};
};
exports.deDupeAsyncFunction = deDupeAsyncFunction;
//# sourceMappingURL=deDupeAsyncFunction.js.map
;