Search code examples
javascriptasynchronouspouchdbecmascript-2016

What is the syntax for await with an event handler for pouchdb?


I want to rewrite the following using async/await

db.replicate.from(remoteCouch).on(
  'complete', (info) => {
     db.sync(remoteCouch, { live: true, retry: true })
   })

How do I handle the on event using await?


Would

var res = await db.replicate.from(remote)
res.on('complete',  (info) => {
   db.sync(remote, opts)
})

work?

Do I need an extra async/await on the callback?


Solution

  • According to the documentation on replicate(), the object returned by .from() actually satisfies the interface of a Promise.

    In general, API calls made through PouchDB return objects that can work like a Promise, according to the API Overview:

    If you don’t specify a callback, then the API returns a promise. In supported browsers or Node.js, native promises are used, falling back to the minimal library lie as needed.