Skip to content

Instantly share code, notes, and snippets.

@hzq1988a
Created June 20, 2017 06:37
Show Gist options
  • Save hzq1988a/a60d1e56f349fe0ea1b3cb47c8c6f8f6 to your computer and use it in GitHub Desktop.
Save hzq1988a/a60d1e56f349fe0ea1b3cb47c8c6f8f6 to your computer and use it in GitHub Desktop.
可以缓存promise结果的辅助函数
export var memoizePromise = function(fun){
var cache = {};
return function(...args){
var key = args.toString();
return new Promise(function(resolve, reject){
if(cache[key]){
return resolve(cache[key])
}else{
fun.apply(this, args).then((data)=>{
cache[key] = data;
resolve(data)
}, (err)=>{
reject(err)
})
}
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment