async fetch()

in src/models/basic/remote.js [91:125]


  async fetch() {
    this._loadRequired = false;
    if (!this._fetchPromise) {
      this._fetchPromise = new Promise(async (resolve) => {
        this._pending = true;
        const {getPrefix, fetchOptions} = this.constructor;
        const prefix = getPrefix();
        try {
          await defer();
          let {headers} = fetchOptions;
          if (!headers) {
            headers = {};
          }
          fetchOptions.headers = headers;
          const response = await fetch(`${prefix}${this.url}`, fetchOptions);
          const data = this.constructor.isJson ? (await response.json()) : (await response.blob());
          await this.update({
            data,
            status: response.status,
          });
        } catch (e) {
          this.failed = true;
          this.error = e.toString();
        }

        this._pending = false;
        this._fetchPromise = null;
        resolve();
        if (this.onDataReceived) {
          this.onDataReceived(this);
        }
      });
    }
    return this._fetchPromise;
  }