Memoization of promise-based function

前端 未结 5 691
Happy的楠姐
Happy的楠姐 2020-12-04 02:01

How can I memoize a promise-based function?

Would straightforward memoization of the function suffice?

function foo() {
    return new Promise((reso         


        
5条回答
  •  孤街浪徒
    2020-12-04 02:29

    Yes, that will suffice. Promises are simple return values, which is their great benefit - in contrast to callbacks, where memoisation code would be horrible.

    You only might want to make sure that the memoized promise is uncancellable, if your promise library does support some kind of cancellation. Also notice that this form of memoisation remembers rejections as well, so you can't recover from errors by "trying again".

提交回复
热议问题