javascript - I can't get an ES6 promise to work -


i can't grasp how promises work. figured i'd jump in , try , create 1 see if helps. following returns undefined value (arrtables):

app.get("/gettables", function (req, res) {     var arrtables = gettables().then(function(response) {         console.log("gettables() resolved");         console.log(arrtables.length);         console.log(arrtables[1].id());     }, function(error) {         console.error("gettables() finished error");     }); });  function gettables() {     return new promise(function(resolve, reject) {         while (mlobby.tlbcount() < lobby_size) {             var objtable = new table();             mlobby.addtable(objtable);         }         resolve(mlobby.tables);     }); } 

new table() references custom class makes async database call. i'm trying use promises make sure call resolves before continue in code. can point out i've gone wrong?

here's console output:

gettables() resolved undefined (node:6580) unhandledpromiserejectionwarning: unhandled promise rejection (rejection id:  1): typeerror: cannot read property 'id' of undefined 

edit add: mlobby.tblcount starts out 0, enter while loop.

the problem array variable. gettable method returns nothing , output of method stored in response variable not in arrtables variable. try use response variable instead of arrtables

 gettables().then(function(response) {     var arrtables = response //added     console.log("gettables() resolved");     console.log(arrtables.length);     console.log(arrtables[1].id);     }, function(error) {         console.error("gettables() finished error"); }); 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -