tweb/src/helpers/callbackify.ts

20 lines
495 B
TypeScript
Raw Normal View History

2022-02-08 20:18:01 +01:00
/*
* https://github.com/morethanwords/tweb
* Copyright (C) 2019-2021 Eduard Kuzmenko
* https://github.com/morethanwords/tweb/blob/master/LICENSE
*/
2022-08-04 08:49:54 +02:00
import {Awaited} from '../types';
2022-02-08 20:18:01 +01:00
export default function callbackify<T extends Awaited<any>, R>(
2022-08-04 08:49:54 +02:00
smth: T,
2022-02-08 20:18:01 +01:00
callback: (result: Awaited<T>) => R
2023-01-06 20:27:29 +01:00
): T extends Promise<any> ? Promise<Awaited<R>> : R {
if(smth instanceof Promise) {
// @ts-ignore
return smth.then(callback);
} else {
2023-01-06 20:27:29 +01:00
return callback(smth as any) as any;
}
}