Pete Pete - 3 years ago 145
Javascript Question

Waiting until an object is created

I am connecting to peer.js and then assigning a property with the returned value. Currently I am using a timeout, but I was wondering if there is a way to just assign the property the value when it has been returned? Kind of how observables work.

this.peer = new Peer({ key: 'mykey' });
setTimeout(() => {
this.myId =;
}, 3000);

Answer Source

use the on open event like this

var peer = new Peer({ key: 'mykey' });
peer.on('open', function(id) {
  console.log('The ID is: ' + id);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download