asynchronously define javascript object property using Promise - Javascript Object

Javascript examples for Object:Object Property

Description

asynchronously define javascript object property using Promise

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=function(){//from   w  w  w.  j  a va 2  s. c o m
function returnName() {
  return new Promise(function(resolve, reject) {
    setTimeout(function() {
       return resolve('John Wick');
    }, 3000);
  });
}
let person = { age: 23 };
returnName().then(function(name) {
  person.name = name;
  console.log(person);
});
    }

      </script> 
   </head> 
   <body>  
   </body>
</html>

Related Tutorials