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

Possible to return the first result in the promise? #9

Open
chichilatte opened this issue Apr 1, 2019 · 0 comments
Open

Possible to return the first result in the promise? #9

chichilatte opened this issue Apr 1, 2019 · 0 comments

Comments

@chichilatte
Copy link

chichilatte commented Apr 1, 2019

Hi. Firstly thanks for this great little plugin!

2ndly: I'm trying to figure out how to get the first result (i.e. the first complete aggregate) from a liveFind() call.
It looks i call liveFind(query) then each doc will be returned as an update event with an ADD action, which we have to debounce (new concept to me, thanks 🙂). E.g.

db.liveFind(query)
.on('ready', () => {
  console.log(`Ready`)
})
.on('update', (update, aggregate) => {
  debouncedRefreshUI(aggregate)
})

In lots of my use-cases we show a spinner and have a few queries to await, then we populate the UI. It would be nice if the first result was passed in the liveFind promise resolution, e.g.

async myfunc() {
  let liveFeed = db.liveFind(query1)
  .on('update', (update, query1Aggregate) => { 
      debouncedRefreshUI(query1Aggregate)
   })
  let query1Result = await liveFeed

  // Do some other async stuff we need for the UI
  let query2Result = await db.find(query2) 
  let query3Result = await db.find(query3) 

  // Populate the UI
   refreshUI(query1Result.docs, query2Result.docs, query3Result.docs)
}

My head is hurting imagining how to mix normal and debounced results without a first-result list!

EDIT:
I see liveFind() returns an EventEmitter with added then and catch that are bound to the pouchdb.find() promise under the hood. Not sure if you can await an object like that, hmm.
EDIT:
Just did a quick test – both...
let result = await db.liveFind(query)
and
db.liveFind(query).then( result => { } )
always give result as undefined.

In the same setup db.find(query) gives a proper result. I'm wondering if this is expected behaviour for liveFind?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant