function ajax (name) {
return new Promise((resolve, reject) =>{
setTimeout(() => {
resolve({name: name})
}, 2000)
})
}
function query (name) {
const cache = query.cache || (query.cache = new Map())
if (cache.has(name)) {
console.log(‘缓存里已有’)
return Promise.resolve(cache.get(name))
}
return ajax(name).then((res) => {
cache.set(name, res)
console.log(cache)
return res
})
}
query (‘Andy’).then((res) => {
console.log(res)
})
setTimeout (() => {
query (‘Andy’).then((res) => {
console.log(res)
})
}, 5000)