Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
117 committed Aug 11, 2024
0 parents commit 5d93bad
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 0 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: deploy
on:
push:
branches:
- main
jobs:
publish:
runs-on: ubuntu-latest
permissions:
contents: read
id-token: write
steps:
- uses: actions/checkout@v4
- name: set up Deno
uses: denoland/setup-deno@v1
with:
deno-version: v1.x
- name: run Deno tests
run: deno test
- name: publish to JSR
run: npx jsr publish
20 changes: 20 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
MIT License

Copyright (c) 2024 github.com/117

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# sleep

![version](https://img.shields.io/jsr/v/%40117/sleep?style=flat-square&color=%23ff51bc&label=version)
![status](https://img.shields.io/github/actions/workflow/status/117/sleep/deploy.yml?style=flat-square)

A promise-based sleep function for Deno.

## Contents

- [Features](#features)
- [Install](#install)
- [Example](#example)
- [Contributing](#contributing)

## Features

- [x] Minimal and easy to use.
- [x] Can be used to `await` for a specified amount of time.

## Install

For Deno:

```sh
$ deno add @117/sleep
```

## Example

```ts
import { sleep } from "@117/sleep";

const work = async () => {
console.log("please wait for 2 seconds");
await sleep(2000);
console.log("thanks for waiting");
};

work();
```

## Contributing

Feel free to contribute and PR to your 💖's content.
10 changes: 10 additions & 0 deletions deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "@117/sleep",
"description": "A promise-based sleep function for Deno.",
"version": "1.0.0",
"exports": "./mod.ts",
"imports": {
"@/": "./",
"assert": "https://deno.land/[email protected]/assert/mod.ts"
}
}
38 changes: 38 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { sleep } from "@/src/sleep.ts";
12 changes: 12 additions & 0 deletions src/sleep.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/** @module sleep */

/**
* Pauses the execution for a specified number of milliseconds.
*
* @param {number} milliseconds - The duration for which to pause the execution, in milliseconds.
* @returns {Promise<void>} A promise that resolves after the specified duration.
*/
const sleep = (milliseconds: number): Promise<void> =>
new Promise((resolve) => setTimeout(resolve, milliseconds));

export { sleep };
16 changes: 16 additions & 0 deletions src/sleep_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { assert } from "assert";

import { sleep } from "@/src/sleep.ts";

Deno.test("does not resolve before the delay", async () => {
const start = Date.now();
const delay = 100;

await sleep(delay);

const end = Date.now();
const elapsed = end - start;

// check if the elapsed time is at least the delay
assert(elapsed >= delay);
});

0 comments on commit 5d93bad

Please sign in to comment.