1
2
3
4
5
6
7
8
9
10
11
12
13
export function promiseHandles<T>() {
let resolve: (value: T | PromiseLike<T>) => void,
reject: (reason?: any) => void;
const promise = new Promise<T>((_resolve, _reject) => {
resolve = _resolve;
reject = _reject;
});
return {
resolve,
reject,
promise,
};
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
3
vladimyr avatar

@postpostscript I guess you are using this inside your private vals but the good news is that you don't need to roll your own implementation! It became a standard thing: Promise.withResolvers().

Checkout out an example: https://www.val.town/v/vladimyr/promiseWithResolvers_example

postpostscript avatar

@vladimyr ahh I was sure I saw something like this somewhere, thanks!

vladimyr avatar

Honestly, I'm amazed how quickly it went through all TC39 proposal stages. The whole thing started last February: https://github.com/tc39/proposal-promise-with-resolvers/commit/c89fa6b 😮

April 10, 2024