Skip to content

Commit

Permalink
feat: adds wait fn
Browse files Browse the repository at this point in the history
  • Loading branch information
OctoD committed Mar 16, 2021
1 parent 5560df3 commit f9aceea
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/__tests__/applicative.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { array, num } from '..';
import { check, bind, fallback, panic, pass, callfnwith } from "../applicative";
import { check, bind, fallback, panic, pass, callfnwith, wait } from "../applicative";
import { pipe } from '../pipe';
import { pipeasync } from '../pipe-async';

describe("applicative", () => {
it("bind", () => {
Expand Down Expand Up @@ -43,4 +44,22 @@ describe("applicative", () => {

expect(pass(value)).toBe(value);
})

it(`wait`, async () => {
let result1: number = 0;
let result2: number = 0;

const testfn1 = async () => result1 = Date.now();
const testfn2 = async () => result2 = Date.now();
const timeout = 1000;
const fn = pipeasync(
testfn1,
bind(wait, timeout),
testfn2,
);

await fn();

expect(Math.floor((result2 - result1) / timeout)).toBe(1)
});
});
22 changes: 22 additions & 0 deletions src/applicative.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,25 @@ export const toasync = <Fn extends FnBase>(
): ((...args: ArgsOf<Fn>) => Promise<ReturnType<Fn>>) => async (
...args: ArgsOf<Fn>
) => fn(...args);

/**
* Waits for a given amount of time (milliseconds), then resolves the promise.
* @since 2.13.0
* @example
*
* ```ts
* import { pipeasync, wait } from 'tiinvo';
*
* const logasync = async (message: string) => () => console.log(message);
*
* pipeasync(
* logasync('The next part of the message will be logged in one second'),
* wait(1000),
* logasync('waited one second'),
* )
* ```
*
* @param milliseconds
* @returns
*/
export const wait = (milliseconds: number) => new Promise(resolve => setTimeout(resolve, milliseconds));

0 comments on commit f9aceea

Please sign in to comment.