Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1 @@
* text=auto eol=lf
* text=auto eol=lf
25 changes: 25 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Lint

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
lint:
name: Lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 17

- name: Install dependencies
run: npm ci

- name: Lint
run: npm run lint
74 changes: 74 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
name: Publish new version

on:
push:
tags:
- 'v*'

jobs:
test:
name: Test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 14
- run: npm ci
- run: npm test

release:
name: Create Release
needs: test
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Create Release
id: create_release
uses: actions/create-release@v1
# Note: GITHUB_TOKEN and github.ref is provided by Actions, you do not need to care about it
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ github.ref }}
release_name: Release ${{ github.ref }}
# TODO: Figure out how to automate meaningfull body
body: |
Release ${{ github.ref }}
draft: false
prerelease: false

publish-npm:
name: Publish on NPM package registry
needs: release
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 14
registry-url: https://registry.npmjs.org/
- run: npm ci
- run: npm run build
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}

publish-github:
name: Publish on GitHub package registry
needs: release
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 14
registry-url: https://npm.pkg.github.com/
# Note: This is an awful solution to publish this package as "foo" on NPM and "@hejny/foo" on GitHub
- run: "sed -i -e 's/\\\"name\\\"\\: \\\"/\\\"name\\\": \\\"\\@hejny\\//g' ./package.json"
- run: npm ci
- run: npm run build
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}
40 changes: 40 additions & 0 deletions .github/workflows/test+lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: Test + Lint

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
test:
name: Test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: '16'

- name: Install dependencies
run: npm ci

- name: Test
run: npm run test

lint:
name: Lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: '16'

- name: Install dependencies
run: npm ci

- name: Lint
run: npm run lint
25 changes: 25 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Test

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
test:
name: Test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 17

- name: Install dependencies
run: npm ci

- name: Test
run: npm run test
17 changes: 11 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
# IDEs
/.idea
/.vscode
!/.vscode/terminals.json
!/.vscode/settings.json


/dist
/coverage

# IDEs
.idea
.vscode/*
!.vscode/terminals.json
!.vscode/settings.json
/src/main.ts


node_modules
/node_modules


.~lock*

/secrets.md
15 changes: 7 additions & 8 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
/.github
/.vscode
/.idea
/coverage

# IDEs
.idea
.vscode/*

node_modules

/src
/node_modules
/samples

.~lock*

/secrets.md
26 changes: 13 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@ Install from [NPM](https://www.npmjs.com/package/waitasecond)
npm i waitasecond
```

## 🕛 Await forTime _([setTimeout](https://developer.mozilla.org/en-US/docs/Web/API/setTimeout) equivalent)_
## 🕛 Await forTimeout _([setTimeout](https://developer.mozilla.org/en-US/docs/Web/API/setTimeout) equivalent)_

In JavaScript, there is an elegant way how to write asynchronous code with **async/await** syntax construct. Every internal function and library is heading forward to be compatible with Promises and deprecating its old callback type.
But there are some **relicts from [callback hell](http://callbackhell.com/)** like **setTimeout, requestAnimationFrame,...**. Waitasecond has motivation to turn this into elegant syntax:

```typescript
import { forTime } from 'waitasecond';
import { forTimeout } from 'waitasecond/forTimeout';

console.log(`⏳ This is logged immediately.`);
await forTime(500);
await forTimeout(500);
console.log(`⌛ And this after 500 milliseconds.`);
await forTime(666);
await forTimeout(666);
console.log(`😈 Wow, I have escaped from callback hell`);
```

Expand All @@ -44,7 +44,7 @@ console.log(`😈 Wow, I have escaped from callback hell`);
## 🕧 Await forImmediate _([setImmediate](https://developer.mozilla.org/en-US/docs/Web/API/Window/setImmediate) equivalent)_

```typescript
import { forImmediate } from 'waitasecond';
import { forImmediate } from 'waitasecond/forImmediate';

async function doSomething() {
console.log(`🍏 foo`);
Expand Down Expand Up @@ -80,7 +80,7 @@ _Note: If you want to use an equivalent of [setInterval](https://developer.mozil
With forAnimationFrame you can write nice looking **render**/update/whatever **loops**.

```typescript
import { forAnimationFrame } from 'waitasecond';
import { forAnimationFrame } from 'waitasecond/forAnimationFrame';

while (
true /* ← Normally, this would be 💩 code, but with forAnimationFrame it is a nicer syntax version of requestAnimationFrame*/
Expand All @@ -102,7 +102,7 @@ _Note: This is working only in a browser environment._
forEver function returns a promise which never resolves or rejects. It is an elegant way to test what happened if some part of asynchronous code stuck (for example, fetch call).

```typescript
import { forEver } from 'waitasecond';
import { forEver } from 'waitasecond/forEver';

await forEver();
console.log(`🧟 This will never ever happen.`);
Expand All @@ -117,7 +117,7 @@ console.log(`🧟 This will never ever happen.`);
For example, if you want to run a process every 10 minutes on a server, but PM2 is restarting a server unexpectedly.

```typescript
import { forTimeSynced } from 'waitasecond';
import { forTimeSynced } from 'waitasecond/forTimeSynced';

while (true) {
await forTimeSynced(10 /* Minutes */ * 60 * 1000);
Expand All @@ -133,7 +133,7 @@ while (true) {
## 🕝 Await forValueDefined

```typescript
import { forValueDefined } from 'waitasecond';
import { forValueDefined } from 'waitasecond/forValueDefined';

const firstName = forValueDefined(() => data.firstName);
```
Expand Down Expand Up @@ -162,18 +162,18 @@ await renderToPdf(document.body);
## 🕒 Await forAllImagesInElement

```typescript
import { forAllImagesInElement } from 'waitasecond';
import { forImagesReady } from 'waitasecond/forImagesReady';

await forAllImagesInElement(document.body);
await forImagesReady(document.body);
console.log(`🖼️ Now I can be sure that all images in body are loaded.`);

// ...
await renderToPdf(document.body);
// ...
```

[📖Documentation](https://hejny.github.io/waitasecond/modules.html#forAllImagesInElement)
[💻Code](https://github.com/hejny/waitasecond/blob/main/src/functions/forAllImagesInElement.ts)
[📖Documentation](https://hejny.github.io/waitasecond/modules.html#forImagesReady)
[💻Code](https://github.com/hejny/waitasecond/blob/main/src/functions/forImagesReady.ts)

<!--Contributing-->
<!--⚠️WARNING: This section was generated by https://github.com/hejny/batch-project-editor/blob/main/src/workflows/810-contributing/contributing.ts so every manual change will be overwritten.-->
Expand Down
Loading