open-vault/ui/app/helpers/await.js
Hamid Ghaf 27bb03bbc0
adding copyright header (#19555)
* adding copyright header

* fix fmt and a test
2023-03-15 09:00:52 -07:00

37 lines
884 B
JavaScript

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
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();
}
}
}
}