devgeeks / echidna.js

Crypton is dead, long live Echidna.js
MIT License
4 stars 1 forks source link

Async - how do we know if Echidna is ready? #5

Open devgeeks opened 7 years ago

devgeeks commented 7 years ago

Throws an error:

// assuming a db exists with these parameters and has a doc with an _id of test
var EchidnaDB = require('echidna.js').default
var echidna = new EchidnaDB({username: 'blah', passphrase: 'blah', salt: 'blah'}); 

echidna.pouch.get('test')
  .then(doc => console.log(doc))
  .catch(error => console.log(error));

Doesn't throw an error

var EchidnaDB = require('echidna.js').default
var echidna = new EchidnaDB({username: 'blah', passphrase: 'blah', salt: 'blah'}); 

setTimeout(() => {
  echidna.pouch.get('test')
    .then(doc => console.log(doc))
    .catch(error => console.log(error));
}, 1000);
devgeeks commented 7 years ago

Something asynchronous must be happening here somewhere...

daviddahl commented 7 years ago

Same behavior in node as in browser?

daviddahl commented 7 years ago

so this is synch, in the test.js:

var echidna = new EchidnaDB(options);

devgeeks commented 7 years ago

Yeah, the function returns synchronously, but strangely the DB isn't actually ready immediately after it returns. :/

Wrapping the calls in an echidna.pouch.info() does keep it from being called too early, but not sure if that is much more than just a fancy setTimeout().

devgeeks commented 6 years ago

This feels basically the same as the setTimeout above, but yeah...

echidna.pouch.info().then(info => {
  echidna.pouch.allDocs()
    .then(docs => {
      // do something with docs
    })
    .catch(error => {
      // do something with the caught error
    });
});