You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pouchdb.apache.org by ja...@apache.org on 2026/05/01 10:23:05 UTC

(pouchdb) 04/04: refactor: use async/await, const and arrow function

This is an automated email from the ASF dual-hosted git repository.

janl pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pouchdb.git

commit 2ebf64743d37a7fd583a56a938f19a257d561a6a
Author: Mariano Giménez <ma...@gmail.com>
AuthorDate: Wed Apr 8 10:30:54 2026 +0200

    refactor: use async/await, const and arrow function
---
 tests/integration/test.events.js | 131 +++++++++++++++++++++------------------
 1 file changed, 70 insertions(+), 61 deletions(-)

diff --git a/tests/integration/test.events.js b/tests/integration/test.events.js
index 5ee7cbfd9..2ff33042d 100644
--- a/tests/integration/test.events.js
+++ b/tests/integration/test.events.js
@@ -1,46 +1,55 @@
 'use strict';
 
-var adapters = ['local', 'http'];
+const adapters = ['local', 'http'];
 
-adapters.forEach(function (adapter) {
-  describe('test.events.js-' + adapter, function () {
+adapters.forEach((adapter) => {
+  describe(`test.events.js-${adapter}`, () => {
 
-    var dbs = {};
+    const dbs = {};
 
-    beforeEach(function () {
+    beforeEach(() => {
       dbs.name = testUtils.adapterUrl(adapter, 'testdb');
     });
 
-    afterEach(function (done) {
-      testUtils.cleanup([dbs.name], done);
+    afterEach(async () => {
+      await new Promise((resolve, reject) => testUtils.cleanup([dbs.name], (err) => {
+        return err ? reject(err) : resolve();
+      }));
     });
 
-    it('PouchDB emits creation event', function (done) {
-      PouchDB.once('created', function (name) {
-        name.should.equal(dbs.name, 'should be same thing');
-        done();
+    it('PouchDB emits creation event', async () => {
+      const eventPromise = new Promise((resolve) => {
+        PouchDB.once('created', (name) => {
+          name.should.equal(dbs.name, 'should be same thing');
+          resolve();
+        });
       });
       new PouchDB(dbs.name);
+      await eventPromise;
     });
 
-    it('PouchDB emits destruction event', function (done) {
-      var db = new PouchDB(dbs.name);
-      db.once('destroyed', done);
+    it('PouchDB emits destruction event', async () => {
+      const db = new PouchDB(dbs.name);
+      const destroyedPromise = new Promise((resolve) => db.once('destroyed', resolve));
       db.destroy();
+      await destroyedPromise;
     });
 
-    it('PouchDB emits destruction event on PouchDB object', function (done) {
-      PouchDB.once('destroyed', function (name) {
-        name.should.equal(dbs.name, 'should have the same name');
-        done();
+    it('PouchDB emits destruction event on PouchDB object', async () => {
+      const eventPromise = new Promise((resolve) => {
+        PouchDB.once('destroyed', (name) => {
+          name.should.equal(dbs.name, 'should have the same name');
+          resolve();
+        });
       });
       new PouchDB(dbs.name).destroy();
+      await eventPromise;
     });
 
-    it('PouchDB emits destroyed when using {name: foo}', function () {
-      var db = new PouchDB({name: 'testdb'});
-      return new Promise(function (resolve) {
-        PouchDB.once('destroyed', function (name) {
+    it('PouchDB emits destroyed when using {name: foo}', async () => {
+      const db = new PouchDB({name: 'testdb'});
+      await new Promise((resolve) => {
+        PouchDB.once('destroyed', (name) => {
           name.should.equal('testdb');
           resolve();
         });
@@ -48,70 +57,70 @@ adapters.forEach(function (adapter) {
       });
     });
 
-    it('db emits destroyed on all DBs', function () {
-      var db1 = new PouchDB('testdb');
-      var db2 = new PouchDB('testdb');
+    it('db emits destroyed on all DBs', async () => {
+      const db1 = new PouchDB('testdb');
+      const db2 = new PouchDB('testdb');
 
-      return new Promise(function (resolve) {
-        var called = 0;
-        function checkDone() {
+      await new Promise((resolve) => {
+        let called = 0;
+        const checkDone = () => {
           if (++called === 2) {
             resolve();
           }
-        }
+        };
         db1.once('destroyed', checkDone);
         db2.once('destroyed', checkDone);
         db1.destroy();
       });
     });
 
-    it('3900 db emits destroyed event', function () {
-      var db = new PouchDB('testdb');
-      return new Promise(function (resolve) {
-        db.once('destroyed', function () {
-          resolve();
-        });
+    it('3900 db emits destroyed event', async () => {
+      const db = new PouchDB('testdb');
+      await new Promise((resolve) => {
+        db.once('destroyed', resolve);
         db.destroy();
       });
     });
 
-    it('3900 db emits destroyed event 2', function () {
-      var db = new PouchDB('testdb');
-      return new Promise(function (resolve) {
-        db.once('destroyed', function () {
-          resolve();
-        });
+    it('3900 db emits destroyed event 2', async () => {
+      const db = new PouchDB('testdb');
+      await new Promise((resolve) => {
+        db.once('destroyed', resolve);
         db.destroy();
       });
     });
 
-    it('emit creation event', function (done) {
-      var db = new PouchDB(dbs.name).on('created', function (newDB) {
-        db.should.equal(newDB, 'should be same thing');
-        done();
+    it('emit creation event', async () => {
+      await new Promise((resolve) => {
+        const db = new PouchDB(dbs.name).on('created', (newDB) => {
+          db.should.equal(newDB, 'should be same thing');
+          resolve();
+        });
       });
     });
 
-    it('#4168 multiple constructor calls don\'t leak listeners', function () {
-      for (var i = 0; i < 50; i++) {
+    it('#4168 multiple constructor calls don\'t leak listeners', () => {
+      for (let i = 0; i < 50; i++) {
         new PouchDB(dbs.name);
       }
     });
 
-    it('4922 Destroyed is not called twice', function (done) {
-      var count = 0;
-      function destroyed() {
-        count++;
-        if (count === 1) {
-          setTimeout(function () {
-            count.should.equal(1);
-            PouchDB.removeListener('destroyed', destroyed);
-            done();
-          }, 50);
-        }
-      }
-      PouchDB.on('destroyed', destroyed);
-      new PouchDB(dbs.name).destroy();
+    it('4922 Destroyed is not called twice', async () => {
+      let count = 0;
+      await new Promise((resolve) => {
+        const destroyed = () => {
+          count++;
+          if (count === 1) {
+            setTimeout(() => {
+              count.should.equal(1);
+              PouchDB.removeListener('destroyed', destroyed);
+              resolve();
+            }, 50);
+          }
+        };
+        PouchDB.on('destroyed', destroyed);
+        new PouchDB(dbs.name).destroy();
+      });
     });
 
   });