title: Async setter / getter in JavaScript
body: <p>I'm working with Web Bluetooth which requires a lot of async operations. I've implemented setters and getters for properties such as the device name. Async setters and getters are not supported by default in ES6, so I've used the following workaround as advised <a href="https://github.com/tc39/ecmascript-asyncawait/issues/15" rel="nofollow noreferrer">here</a>:</p>
  
  <pre><code>get name() {
    return ( async () =&gt; {
      try {
        const data = await this._readData(this.nameCharacteristic);
        const decoder = new TextDecoder("utf-8");
        const name = decoder.decode(data);
        return name;
      } 
      catch (error) {
        return error;
      }
    })();
  }
  </code></pre>
  
  <p>And:</p>
  
  <pre><code>set name(name) {
    return ( async (name) =&gt; {
      const byteArray = new Uint8Array(name.length);
      for (let i = 0; i &lt; name.length; i += 1) {
        byteArray[i] = name.charCodeAt(i);
      }
      return await this._writeData(this.nameCharacteristic, byteArray);
    })(name);
  }
  </code></pre>
  
  <p>I can do the following succesfully with the getter:</p>
  
  <pre><code>await device.connect();
  await device.name;
  await device.&lt;some async BLE operation&gt;
  </code></pre>
  
  <p>But the following with the setter results in the reference error "Uncaught ReferenceError: Invalid left-hand side in assignment" for the setter:</p>
  
  <pre><code>await device.connect();
  await device.name = "newName";
  await device.&lt;some async BLE operation&gt;
  </code></pre>
  
  <p>If I remove the <code>await</code> keyword, the error is fixed, but the script will fail because it will attempt two GATT operations at the same time, which is not supported. </p>
  
  <p>So it seems that I can only await a getter and not a setter. Is there something wrong with my implementation of the getter that causes this? Do I have to go back to using a function like <code>device.nameSet()</code> instead of setter when doing async operations? </p>
  
  <p>For the record, <code>_writeData()</code> and <code>_readData()</code> look like this:</p>
  
  <pre><code>async _writeData(characteristic, dataArray) {
    if (!this.bleIsBusy) {
      try {
        this.bleIsBusy = true;
        await characteristic.writeValue(dataArray);
        this.bleIsBusy = false;
      }
      catch (error) {
        return error;
      }
      return Promise.resolve();
    }
    else {
      return Promise.reject(new Error("GATT operation already pending"));
    }
  }
  
  async _readData(characteristic) {
    if (!this.bleIsBusy) {
      try {
        this.bleIsBusy = true;
        const dataArray = await characteristic.readValue();
        this.bleIsBusy = false;
  
        return dataArray;
      }
      catch (error) {
        return error;
      }
    }
    else {
      return Promise.reject(new Error("GATT operation already pending"));
    }
  }
  </code></pre>