Речь о чём-то похожем?
const singleBusinessLogic = (() => {
let isBusy = false;
let promise = null;
let done = null;
let failure = null;
return (id) => {
if (!isBusy) {
isBusy = true;
promise = new Promise((resolve, reject) => ([done, failure] = [resolve, reject]));
fetch(`https://jsonplaceholder.typicode.com/users/${id}`)
.then(response => response.json())
.then(done)
.catch(failure)
.finally(() => (isBusy = false));
}
return promise;
};
})();
(async () => {
console.log(await singleBusinessLogic(1)); // { ..., name: 'Leanne Graham', ... }
console.log(await singleBusinessLogic(2)); // { ..., name: 'Leanne Graham', ... }
setTimeout(async () => {
console.log(await singleBusinessLogic(3)); // { ..., name: 'Clementine Bauch', ... }
console.log(await singleBusinessLogic(4)); // { ..., name: 'Clementine Bauch', ... }
});
})();
Или хотите возвращать результат только самого первого вызова?