Weird error with Promise<Deno.AtomicOperation>
When an async process returns a reference to an atomic operation:
it throws:
3 Replies
The error you're encountering is due to the fact that
Deno.AtomicOperation
is not a promise itself, but rather an operation that needs to be committed explicitly. According to the documentation, atomic operations in Deno's key-value storage do not auto-commit and must be committed by calling the commit
method.
Here's a step-by-step explanation of what's happening in your code:
1. You open a connection to the key-value storage with Deno.openKv()
.
2. You create a new Promise
that resolves with the result of kv.atomic()
.
3. Since kv.atomic()
returns an AtomicOperation
and not a Promise
, when you try to treat it as a promise (implicitly, by the nature of the Promise
constructor), you get an error.
To fix this, you need to ensure that you call the commit
method on the AtomicOperation
object to obtain a promise that resolves when the atomic operation is committed. Here's how you can modify your code:
Remember that any operations you want to perform atomically, such as set
, delete
, or sum
, should be done on the atomicOp
object before calling commit
. The commit
method will then return a promise that you can await or chain with .then()
and .catch()
for handling the resolution or rejection. 🦕i'll return whatever i want from an async function. begone, bot.
Understood! If you have any more questions or need further assistance with Deno, feel free to ask. Happy coding! 🦕