Treetop Flyer
Treetop Flyer3w ago

Weird error with Promise<Deno.AtomicOperation>

When an async process returns a reference to an atomic operation:
const kv = await Deno.openKv();
// contrived async process:
new Promise( ok=>ok(kv.atomic()) );
const kv = await Deno.openKv();
// contrived async process:
new Promise( ok=>ok(kv.atomic()) );
it throws:
error: Uncaught (in promise) TypeError: `Deno.AtomicOperation` is not a promise. Did you forget to call `commit()`?
at AtomicOperation.then (ext:deno_kv/01_db.ts:413:11)
at eventLoopTick (ext:core/01_core.js:168:7)
error: Uncaught (in promise) TypeError: `Deno.AtomicOperation` is not a promise. Did you forget to call `commit()`?
at AtomicOperation.then (ext:deno_kv/01_db.ts:413:11)
at eventLoopTick (ext:core/01_core.js:168:7)
3 Replies
Deno AI Helper
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:
const kv = await Deno.openKv();
// Start an atomic operation
const atomicOp = kv.atomic();
// ... perform operations like set, delete, etc. on atomicOp
// Commit the atomic operation to get a promise
const commitPromise = atomicOp.commit();
// Now you can use the promise
commitPromise.then(() => {
console.log('Atomic operation committed successfully');
}).catch((error) => {
console.error('Error committing atomic operation:', error);
});
const kv = await Deno.openKv();
// Start an atomic operation
const atomicOp = kv.atomic();
// ... perform operations like set, delete, etc. on atomicOp
// Commit the atomic operation to get a promise
const commitPromise = atomicOp.commit();
// Now you can use the promise
commitPromise.then(() => {
console.log('Atomic operation committed successfully');
}).catch((error) => {
console.error('Error committing atomic operation:', error);
});
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. 🦕
Treetop Flyer
Treetop Flyer3w ago
i'll return whatever i want from an async function. begone, bot.