Created
June 20, 2017 06:37
-
-
Save hzq1988a/a60d1e56f349fe0ea1b3cb47c8c6f8f6 to your computer and use it in GitHub Desktop.
可以缓存promise结果的辅助函数
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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