planttheidea/micro-memoize

No memoization for asynchronous function with no inputs

Closed this issue · 2 comments

aminya commented

I am using micro-memoize to skip doing work in the future calls for some async functions. However, the function runs again in the subsequent calls.

I cannot reproduce it in a simple example like the following,

import memoize from 'micro-memoize';

function sleep(n) {
  return new Promise(resolve => setTimeout(resolve, n));
}

async function doSomething_raw() {
  console.log("running...")
  await sleep(1000)
  return "hey"
}
const doSomething = memoize(doSomething_raw, { isPromise: true })


await doSomething()
await doSomething()

But in the complex project, I am seeing the function running two times:
https://github.com/aminya/setup-cpp/blob/771f77f24f8e63f91c375b6bba0f861ca7ea16c8/src/utils/setup/setupPipPack.ts#L86-L93

https://github.com/aminya/setup-cpp/actions/runs/6114633731/job/16596651001?pr=198#step:8:2476
https://github.com/aminya/setup-cpp/actions/runs/6114633731/job/16596651001?pr=198#step:8:3043

One of the differences is that there are nested memorized async functions in the complex function.

Honestly, this is going to be pretty difficult to debug if you cannot provide a simple repro, especially if the one you provide specifically does not reproduce the issue. 😅

Something to note with promise-based setups is that if the promise throws it will remove the memoized version from cache. This is an intentional decision, however if you do not want that automatic removal and if you do not have any cache change listeners set up, then you can just memoize the function as a standard method. Details of this can be found in the documentation.

Closing because no activity has transpired in months. If you have new information to provide, please reopen.

One thing I'll note: looking at the code referenced, an error is thrown in certain conditions. If this condition is met (pythonBin is undefined) then it would trigger the automatic removal from cache, which would create the scenario you mention. I would start there, validating that pythonBin is always populated in these scenarios.