Here is a code snippet to consider:
class ClientWrapper {
private client: Client;
constructor() {
this.client = new Client();
}
async connect() : Promise<void> {
return this.client.connect();
}
async isConnected(): Promise<boolean> {
return this.client.isConnected();
}
};
class Client {
private data?: string;
private connected: boolean;
constructor() {
this.connected = false;
}
isConnected(): boolean {
return this.connected;
}
async connect() : Promise<void> {
this.data = 'data';
const res = await this.executeRequest();
this.connected = true;
}
async executeRequest() : Promise<string> {
return await Promise.resolve(this.data!);
}
};
let wrapper = new ClientWrapper();
(async () => {
await wrapper.connect();
console.log(await wrapper.isConnected());
})();
At line 48, the output of
console.log(await wrapper.isConnected())
is true
.
Now, if we change the connect()
method in ClientWrapper
to:
async connect() : Promise<void> {
this.client.connect();
}
, where the return
statement is removed.
After this modification, line 48 now outputs false
.
The question then arises - why does the connected
property of the Client
class not retain the value of true
? Does the absence of the return
statement in the Promise<void>
matter for this behavior?
Thank you!