Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Coalescing multiple load() calls #349

Closed
ovaris opened this issue Aug 25, 2023 · 3 comments
Closed

Coalescing multiple load() calls #349

ovaris opened this issue Aug 25, 2023 · 3 comments

Comments

@ovaris
Copy link

ovaris commented Aug 25, 2023

I'd like to use Dataloader with await's, but the problem is that batching doesn't work, I guess it has something to do with ticks & event loop. Could someone give me a working example where I could use await's to call load() functions so that it would still use batching?

A simple test.js script:

const DataLoader = require('dataloader')

const testLoader = new DataLoader((ids) => {
  console.log('testLoader', ids)
  return Promise.resolve(ids)
})

const dummyFunction = async () => {
  await testLoader.load(1) // <--- DOES NOT BATCH
  await testLoader.load(2)

  await Promise.all([testLoader.load(3), testLoader.load(4)]) // <-- BATCHES
}

;(async () => {
  await dummyFunction()
})()

output:

node --version
v16.20.2
node test.js
testLoader [ 1 ]
testLoader [ 2 ]
testLoader [ 3, 4 ]

I have a async Express handler function where I'd like to use loader with await's to load stuff from DB, so really basic stuff.
Any tips appreciated!

@ovaris ovaris changed the title Batching and event loops Coalescing multiple load() calls Aug 25, 2023
@joewestcott
Copy link

If you use the await keyword, execution will pause at that step until completion, hence, no batching.

@ardatan
Copy link
Member

ardatan commented Aug 29, 2023

You cannot expect batching to work in your scenario. To understand better, you can ask this question "What if first await's result is needed in the following?"

  const res =  await testLoader.load(1) 
  await testLoader.load(res)

So it is not possible to use batching like that.

@ovaris
Copy link
Author

ovaris commented Aug 30, 2023

Yeah, thanks, makes perfectly sense, closing this issue.

@ovaris ovaris closed this as completed Aug 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants