* loadAssetAsync * Returns a Promise to fetch the data identified by the key. * @param {string} key - identifier for the data, should be found in the asset map. * @return {Promise} Promise resolved with the data
(key)
| 252 | * @return {Promise} Promise resolved with the data |
| 253 | */ |
| 254 | loadAssetAsync(key) { |
| 255 | if (this._cache[key]) { |
| 256 | return Promise.resolve(this._cache[key]); |
| 257 | } |
| 258 | |
| 259 | let url; |
| 260 | try { |
| 261 | url = this.getAssetURL(key); |
| 262 | } catch (err) { |
| 263 | return Promise.reject(err.message); |
| 264 | } |
| 265 | |
| 266 | let loadPromise = this._inflight[url]; |
| 267 | if (!loadPromise) { |
| 268 | this._inflight[url] = loadPromise = fetch(url) |
| 269 | .then(utilFetchResponse) |
| 270 | .then(result => { |
| 271 | delete this._inflight[url]; |
| 272 | if (!result) { |
| 273 | throw new Error(`No data loaded for "${key}"`); |
| 274 | } |
| 275 | this._cache[key] = result; |
| 276 | return result; |
| 277 | }) |
| 278 | .catch(err => { |
| 279 | delete this._inflight[url]; |
| 280 | throw err; |
| 281 | }); |
| 282 | } |
| 283 | |
| 284 | return loadPromise; |
| 285 | } |
| 286 | |
| 287 | } |
no test coverage detected