blinkdb-js / blinkdb

🗃️ An in-memory JS database optimized for large scale storage on the frontend.
https://blinkdb.io
MIT License
119 stars 10 forks source link

Docs: watch is async #9

Closed llamadeus closed 1 year ago

llamadeus commented 1 year ago

In the example code in the docs, the return value of watch is being used as if the function ran synchronously, where in reality it returns a Promise. Also, if watch is not awaited, inserting items cause race conditions and will not be properly handled by the callback passed to watch.

Wrong (how it is currently)

const watcher = watch(userTable, (users) => {
  console.log("All users: ", users);
});

watcher.stop();

Correct

const watcher = await watch(userTable, (users) => {
  console.log("All users: ", users);
});

watcher.stop();