promises helpers now ts

This commit is contained in:
iPromKnight
2024-02-05 05:02:41 +00:00
committed by iPromKnight
parent 948cb8e037
commit 9e9e928c80
5 changed files with 55 additions and 58 deletions

View File

@@ -1,55 +0,0 @@
/**
* Execute promises in sequence one after another.
*/
export async function sequence(promises) {
return promises.reduce((promise, func) =>
promise.then(result => func().then(Array.prototype.concat.bind(result))), Promise.resolve([]));
}
/**
* Return first resolved promise as the result.
*/
export async function first(promises) {
return Promise.all(promises.map((p) => {
// If a request fails, count that as a resolution so it will keep
// waiting for other possible successes. If a request succeeds,
// treat it as a rejection so Promise.all immediately bails out.
return p.then(
(val) => Promise.reject(val),
(err) => Promise.resolve(err)
);
})).then(
// If '.all' resolved, we've just got an array of errors.
(errors) => Promise.reject(errors),
// If '.all' rejected, we've got the result we wanted.
(val) => Promise.resolve(val)
);
}
/**
* Delay promise
*/
export async function delay(duration) {
return new Promise((resolve) => setTimeout(resolve, duration));
}
/**
* Timeout promise after a set time in ms
*/
export async function timeout(timeoutMs, promise, message = 'Timed out') {
return Promise.race([
promise,
new Promise(function (resolve, reject) {
setTimeout(function () {
reject(message);
}, timeoutMs);
})
]);
}
/**
* Return most common value from given array.
*/
export function mostCommonValue(array) {
return array.sort((a, b) => array.filter(v => v === a).length - array.filter(v => v === b).length).pop();
}

View File

@@ -0,0 +1,52 @@
/**
* Execute promises in sequence one after another.
*/
export async function sequence(promises: Array<() => Promise<any>>): Promise<any[]> {
return promises.reduce((promise: Promise<any[]>, func: () => Promise<any>) =>
promise.then(result => func().then(res => result.concat(res))), Promise.resolve([]));
}
/**
* Return first resolved promise as the result.
*/
export async function first(promises: Array<Promise<any>>): Promise<any> {
return Promise.all(promises.map(p => {
// If a request fails, count that as a resolution so it will keep
// waiting for other possible successes. If a request succeeds,
// treat it as a rejection so Promise.all immediately bails out.
return p.then((val) => Promise.reject(val), (err) => Promise.resolve(err));
})).then(
// If '.all' resolved, we've just got an array of errors.
(errors) => Promise.reject(errors),
// If '.all' rejected, we've got the result we wanted.
(val) => Promise.resolve(val)
);
}
/**
* Delay promise
*/
export async function delay(duration: number): Promise<void> {
return new Promise((resolve) => setTimeout(() => resolve(), duration));
}
/**
* Timeout promise after a set time in ms
*/
export async function timeout(timeoutMs: number, promise: Promise<any>, message = 'Timed out'): Promise<any> {
return Promise.race([
promise,
new Promise(function (resolve, reject) {
setTimeout(function () {
reject(message);
}, timeoutMs);
})
]);
}
/**
* Return most common value from given array.
*/
export function mostCommonValue(array: any[]): any {
return array.sort((a, b) => array.filter(v => v === a).length - array.filter(v => v === b).length).pop();
}

View File

@@ -1,7 +1,7 @@
import { parse } from 'parse-torrent-title';
import { getImdbId, getKitsuId } from './metadata';
import { isPackTorrent } from './parseHelper.js';
import * as Promises from './promises.js';
import * as Promises from './promises';
import { repository } from '../repository/database_repository';
import { parseTorrentFiles } from './torrentFiles.js';
import { assignSubtitles } from './torrentSubtitles.js';

View File

@@ -6,7 +6,7 @@ import { metadataConfig } from './config.js';
import { isDisk } from './extension.js';
import { getMetadata, getImdbId, getKitsuId } from './metadata';
import { parseSeriesVideos, isPackTorrent } from './parseHelper.js';
import * as Promises from './promises.js';
import * as Promises from './promises';
import {torrentFiles} from "./torrent.js";
import { TorrentType } from './enums/torrent_types';
import {logger} from "./logger";

View File

@@ -2,7 +2,7 @@ import moment from 'moment';
import {literal, Op, WhereOptions} from "sequelize";
import {Model, Sequelize} from 'sequelize-typescript';
import {databaseConfig} from '../lib/config';
import * as Promises from '../lib/promises.js';
import * as Promises from '../lib/promises';
import {Provider} from "./models/provider";
import {File} from "./models/file";
import {Torrent} from "./models/torrent";