JSFiddle - React, Tailwind, and code Playground
JavaScript
'use strict';
class Server {
helloWorld() {
return "Hello world"
}
}
class Client {
constructor() {
this.connection = null
this.establishConnection()
}
establishConnection() {
//this.connection= new Server()
// simulate slow connection setup by initializing after 2 seconds
setTimeout(() => {this.connection= new Server()}, 2000)
}
doSomethingRemote() {
console.log(this.connection.helloWorld())
}
}
let test = new Client();
test.establishConnection();
// doesn't work because we try immediately after object initialization
test.doSomethingRemote();
// works because the object has had time to initialize
setTimeout(() => {test.doSomethingRemote()}, 3000)