Home pass object reference into javascript async function, but the unexpected value gotten
Reply: 0

pass object reference into javascript async function, but the unexpected value gotten

user1112
1#
user1112 Published in June 18, 2018, 8:46 pm

I use the object as the function parameter to try saving out the value of the consequence of a async function. Code snippets shown bellow,

function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms));}
...

let u={};
async function exec(){
     getCurrUser(action.w,u);//get current user login data from indexeddb io
     await sleep(2000);
};
exec().then(()=>{console.log(u); /*u is still {}, but WHY ?*/ });   });

getCurrentUser is something like

export function getCurrUser(window,user) {
   ...
 _req.onsuccess=function(event){
                let cursor=event.target.result;
                //confirm in this line that user object does has some value
                if(cursor) {user = Object.assign({}, cursor);return;}
                else {return;}
            };
  ...

}

My understanding is that if the timeout is long enough to secure the valid execution of getCurrUser, I can get the final function output through the 2nd paramter reference. But the fact is not so. Why? Any ideas are welcome.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.260351 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO