thunderhub/server/helpers/async.ts
Anthony Potdevin bc4415cde7
chore: 🔧 enable strict tsconfig (#96)
* chore: 🔧 strict enable wip

* Improve typings, add type dependencies (#97)

We improve the TypeScript situation by enabling `noImplicitAny`. This
leads to a bunch of errors, as expected. We fix some of these by
installing the correct types, and some are fixed manually by an educated
guess. There are still a lot left, and these seem to be a big mix of
lacking types for the `ln-service` dependency and potential bugs.

* Strict null (#100)

* chore: 🔧 enable strict-null

* chore: 🔧 more checks

* chore: 🔧 some fixes

* chore: 🔧 more types

* chore: 🔧 more types

* chore: 🔧 more types

* chore: 🔧 more types

* chore: 🔧 more types

* chore: 🔧 more types

* chore: 🔧 more types

* chore: 🔧 enable strict

* fix: 🐛 input

* fix: 🐛 forward report

* fix: 🐛 chat sending

* fix: 🐛 chat bubble input

Co-authored-by: Torkel Rogstad <torkel@rogstad.io>
2020-08-05 08:35:26 +02:00

24 lines
760 B
TypeScript

import { getErrorMsg } from './helpers';
import { logger } from './logger';
export const to = async <T>(promise: Promise<T>) => {
return promise
.then(data => data)
.catch(err => {
logger.error('%o', err);
throw new Error(getErrorMsg(err));
});
};
/*
* This is hard/impossible to type correctly. What we are describing
* here is a set of two states: either we have a result and no error,
* _or_ we have no result and an error. Unfortunately TypeScript is
* not able to infer this correctly...
* https://github.com/microsoft/TypeScript/issues/12184
*/
export const toWithError = async <T>(promise: Promise<T>) => {
return promise
.then(data => [data, undefined] as const)
.catch(err => [undefined, err] as const);
};