I have a question regarding promises in nodeJS when using Rethindb. This code will give me the results from db first time anything changes, not more.
Lets say I start the script and add a row to the db, the new data will be printed in cmd. But if I add another one, nothing are shown. Something wrong with the way I use the promise? (and no, I dont want to use callback)
Thx!
PushData(r, table)
.then(res=>{
console.log(res);
}
function PushData(r, table){
return new Promise(function(resolve, reject){
r.table(table)
.changes()
.run()
.then(function(cursor){
cursor.on("error", function(err) {
reject(err)
})
cursor.on("data", function(data) {
resolve(data);
})
});
});
}
Yes, you are conflating "promises" with "callbacks". A fundamental principle of a Promise is that it can only be fulfilled ONCE. After it has been resolved or rejected (the two ways it can be fulfilled), it cannot be operated on again.
Callbacks are not evil. This is the time to use them.