JSFiddle - React, Tailwind, and code Playground

by Alexey Ukolov

JavaScript

var Foo = function(url) {
  this.url = url;

  this.connection = this.connect();

  Object.defineProperty(this, 'onopen', {
    set: function(cb) {
      this.connection.then(cb.bind(this));
    }
  });
}

Foo.prototype.connect = function() {
  console.log('connecting to %s', this.url);

  return new Promise(function(resolve, reject) {
    setTimeout(function () {
    	console.log('connected');
    	resolve()
    }, 2000);
  });
}



var socket = new Foo('example.com:8081');

socket.onopen = function() {
  console.log('connection to %s established', this.url);
}