Pete Pete - 1 month ago 10
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 = this.peer.id;
}, 3000);

Answer

use the on open event like this

var peer = new Peer({ key: 'mykey' });
peer.on('open', function(id) {
  console.log('The ID is: ' + id);
});
Comments