Skip to content

Commit 85323f8

Browse files
Bring back the completable promise from the past
1 parent 071438f commit 85323f8

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

packages/restate-sdk/src/endpoint/handlers/generic.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,8 @@ export class GenericHandler implements RestateHandler {
291291
(bytes) => {
292292
coreVm.sys_write_output_success(bytes);
293293
coreVm.sys_end();
294-
},
295-
(e) => {
294+
})
295+
.catch((e) => {
296296
const error = ensureError(e);
297297
console.warn("Function completed with an error.\n", error);
298298

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright (c) 2023-2024 - Restate Software, Inc., Restate GmbH
3+
*
4+
* This file is part of the Restate SDK for Node.js/TypeScript,
5+
* which is released under the MIT license.
6+
*
7+
* You can find a copy of the license in file LICENSE in the root
8+
* directory of this repository or package, or at
9+
* https://github.com/restatedev/sdk-typescript/blob/main/LICENSE
10+
*/
11+
12+
// Like https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/withResolvers
13+
// (not yet available in node)
14+
export class CompletablePromise<T> {
15+
private success!: (value: T | PromiseLike<T>) => void;
16+
private failure!: (reason?: unknown) => void;
17+
18+
public readonly promise: Promise<T>;
19+
20+
constructor() {
21+
this.promise = new Promise((resolve, reject) => {
22+
this.success = resolve;
23+
this.failure = reject;
24+
});
25+
}
26+
27+
public resolve(value: T) {
28+
this.success(value);
29+
}
30+
31+
public reject(reason?: unknown) {
32+
this.failure(reason);
33+
}
34+
}

0 commit comments

Comments
 (0)