Queue of a single item, only once, allows for simple synchronisation.
It has a 'first write wins' behaviour
const q = new WaitForValue(); // or singleItem();// In some part of the code add a valueconst value = q.add(`some-val`);// Somewhere else, wait for valueawait q.get(value); Copy
const q = new WaitForValue(); // or singleItem();// In some part of the code add a valueconst value = q.add(`some-val`);// Somewhere else, wait for valueawait q.get(value);
It is not possible to add a second item (an exception will throw), however it is possible to call get as many times as you need.
add
get
The .isUsed property allows you to to check if a value has been already added to the queue.
.isUsed
Based on: https://2ality.com/2024/05/proposal-promise-with-resolvers.html
Returns true if a value has been added and therefore no more values can be written
Adds a value, triggering promise resolution.
Throws an exception if queue has already been used. Use isUsed to check.
Gets the promise
const wv = new WaitForValue();await wv.get(); Copy
const wv = new WaitForValue();await wv.get();
Queue of a single item, only once, allows for simple synchronisation.
It has a 'first write wins' behaviour
It is not possible to
add
a second item (an exception will throw), however it is possible to callget
as many times as you need.The
.isUsed
property allows you to to check if a value has been already added to the queue.Based on: https://2ality.com/2024/05/proposal-promise-with-resolvers.html