The type of successful value
The type of error value
A Promise that resolves to a Result containing either success or error
// Create a promise that resolves successfully
const successPromise = createPromise<number, string>((resolve) => {
setTimeout(() => {
resolve(42);
}, 0);
});
const successResult = await successPromise;
assert.ok(Result.isOk(successResult));
if (Result.isOk(successResult)) {
assert(successResult.value === 42);
}
// Create a promise that rejects with an error
const errorPromise = createPromise<number, string>((_, reject) => {
setTimeout(() => {
reject('Something went wrong');
}, 0);
});
const errorResult = await errorPromise;
assert.ok(Result.isErr(errorResult));
if (Result.isErr(errorResult)) {
assert(errorResult.value === 'Something went wrong');
}
Creates a Promise that wraps the result in a Result type for type-safe error handling. This function is an alternative to
new Promise(executor)
that provides enhanced type safety by returning a Result type instead of throwing exceptions.