Skip to content

Instantly share code, notes, and snippets.

@abegehr
Last active May 13, 2020 10:47
Show Gist options
  • Save abegehr/1a55898ba04979e10c40275d4d560711 to your computer and use it in GitHub Desktop.
Save abegehr/1a55898ba04979e10c40275d4d560711 to your computer and use it in GitHub Desktop.
Sleepjs has all standard JS Promise capabilities
// https://github.com/simsieg/sleepjs
const { sleep, sleepSeconds, sleepMinutes } = require('sleepjs')
// chain sleeps of different intervals
async function chainingFunc() {
await sleep(500);
alert("Hello");
await sleepSeconds(1);
alert("Hello");
await sleepSeconds(2);
alert("Hello");
await sleepSeconds(4);
alert("Hello");
}
// execute sleeps simultenously
const simultaneousFunc = async () => Promise.all([
sleep(500).then(() => alert("Hello")),
sleepSeconds(1).then(() => alert("Hello")),
sleepSeconds(2).then(() => alert("Hello")),
sleepSeconds(4).then(() => alert("Hello")),
]);
// integrate with other promises
async function integrateFunc() {
await fetchData(); // this is a promise
await sleepMinutes(1);
await fetchData();
await sleepMinutes(2);
await fetchLaterData();
await sleepMinutes(4);
}
chainingFunc();
simultaneousFunc();
integrateFunc();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment