Files
vault/ui/lib/core/addon/helpers/await.js
claire bontempo 09c92b89cc UI: Add capabilities service (#28168)
* add capabilities service

* remove from kv engine for now

* add canRead

* move await helper to addon

* add test

* update capabilities service to accommodate multiple paths

* address comments, make methods more explicit

* remove namespace key

* fix typo in test

* add namespace back!

* round out tests for other methods

* add test

* add comment
2024-08-23 16:17:19 -07:00

37 lines
885 B
JavaScript

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: BUSL-1.1
*/
import Helper from '@ember/component/helper';
import { Promise } from 'rsvp';
export default class AwaitHelper extends Helper {
compute([promise]) {
if (!promise || typeof promise.then !== 'function') {
return promise;
}
if (promise !== this.lastPromise) {
this.lastPromise = promise;
this.value = null;
this.resolve(promise);
}
return this.value;
}
async resolve(promise) {
let value;
try {
value = await Promise.resolve(promise);
} catch (error) {
value = error;
} finally {
// ensure this promise is still the newest promise
// otherwise avoid firing recompute since a newer promise is in flight
if (promise === this.lastPromise) {
this.value = value;
this.recompute();
}
}
}
}