javascript - Chrome Tab重复项未发出网络请求

标签 javascript reactjs browser window

我在应用程序加载(componentDidMount)上有3个api调用。
当我重新加载页面或打开新选项卡时,会进行所有api调用,但在复制选项卡时仅会触发其中一个。
如何触发页面复制中的所有api调用
注意:
浏览器:chrome。

最佳答案

使用Memopromise工具是处理异步过程的有效工具。

class MemoPromise {

 constructor(getPromise) {

   this.cache = {};

   this.getPromise = getPromise;

   this.request = this.request.bind(this);

 }

 request({ uniqueKey, ...rest }) {

   if (!uniqueKey) {

     return Promise.reject(new Error('Unique key not passed'));

   }

   if (this.cache[uniqueKey]) {

     return this.cache[uniqueKey];

   }

   const promise = this.getPromise(rest);

   this.cache[uniqueKey] = promise

     .then((res) => {

       delete this.cache[uniqueKey];

       return res;

     })

     .catch((err) => {

       delete this.cache[uniqueKey];

       throw err;

     });

   return this.cache[uniqueKey];

 }

}
在上面的示例中,我创建了一个MemoPromise类,该类的实例化对象可以记住构造函数中传递的函数返回的promise,直到它们未被解析或拒绝为止。
查看执行代码
const memoPromise = new MemoPromise(fn);

// invocation

const { request } = memoPromise;

request({ uniqueKey: url, apiType, url, payload });

// not required now

// fn({ apiType, url, payload });
在将Memopromise类集成到您的浏览器之后,您的API请求可能不会遇到任何问题。它可以有效地处理您的重复请求。

关于javascript - Chrome Tab重复项未发出网络请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63445841/

相关文章:

javascript - 当用户在其外部单击时,使用 jquery 隐藏自定义模式

javascript - Twitter bootstrap 2.3.2 弹出窗口在悬停时保持打开状态

javascript - React 中的子方法可以有更改处理程序吗?

所有浏览器中的 JavaScript 和 jQuery 滚动问题

javascript - 将内联函数表达式作为参数传递给常规函数

javascript - 在 Angular 中隐藏数组中的不匹配元素

javascript - React中array.map和onchange的使用

javascript - 在 componentdidmount 之前调用 React render

android - android中的浏览器检测

c# - 如何将从网站下载的数据转换为正确的编码?