Pete Pete - 9 months ago 64
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);