layout: post title: 如何在多个模块中共享异步数据
<html>
<head>
<title>HTML</title>
</head>
<body>
<script src="./script1.js"></script>
<script src="./script2.js"></script>
</body>
</html>
script1
中有一个异步请求,script2
也想使用请求得到的数据。
script1.js
:
const fetchRemoteData = () => {
return new Promise((resolve) => {
console.log('send request');
setTimeout(() => {
resolve({ message: 'OK' });
}, 3000);
});
};
fetchRemoteData().then((data) => {
console.log('Use data in script1', data);
});
scripts.js
:
fetchRemoteData().then((data) => {
console.log('Use data in script2', data);
});
这里用 fetchRemoteData
表示接口请求。
上述写法会导致请求发送两次。
script2
检查数据是否准备好script1
:
const fetchRemoteData = () => {
return new Promise((resolve, reject) => {
console.log('send request');
setTimeout(() => {
resolve({ message: 'OK' });
}, 3000);
});
};
let savedData = null;
fetchRemoteData().then((data) => {
savedData = data;
console.log('Use data in script1', data);
});
script2
:
const checkInterval = setInterval(() => {
if (savedData) {
console.log('Use data in script2', savedData);
clearInterval(checkInterval);
}
}, 100);
只发送一次请求,script2
中的数据获取性能较差。
script1
:
const fetchRemoteData = () => {
return new Promise((resolve, reject) => {
console.log('send request');
setTimeout(() => {
resolve({ message: 'OK' });
}, 3000);
});
};
const whenDataReady = (() => {
let savedData = null;
let callbacks = [];
fetchRemoteData().then((data) => {
savedData = data;
while (callbacks.length) {
const callback = callbacks.pop();
callback(savedData);
}
});
return (callback) => {
if (savedData) {
callback(savedData);
} else {
callbacks.push(callback);
}
};
})();
whenDataReady((data) => {
console.log('Use data in script1', data);
});
script2
:
whenDataReady((savedData) => {
console.log('Use data in script2', savedData);
});
只发送一次请求。
script1
:
const fetchRemoteData = () => {
return new Promise((resolve, reject) => {
console.log('send request');
setTimeout(() => {
resolve({ message: 'OK' });
}, 3000);
});
};
const fetchRemoteDataPromise = fetchRemoteData();
fetchRemoteDataPromise.then((data) => {
console.log('Use data in script1', data);
});
script2
:
fetchRemoteDataPromise.then((data) => {
console.log('Use data in script2', data);
});
只发送一次请求。