Delay
If the call is not await
ed, execution continues:
// Schedule 'someFn' after 100msdelay(someFn, 100);// ...but execution continues here immediately
By default the delay period is before running the supplied function, but it can also be after:
// Runs 'someFn' immediatelyawait delay(someFn, { delay: "after", secs: 10 });// ...but execution does not continue here until 10 seconds later
Or both:
// Waits 10seconds, and then runs `someFn`await delay(someFn, { delay: "both", secs: 10 });// ...and waits a further 10secs before continuing here
Compared to ‘sleep’
Flow.delay is very similar to sleep
, but lets you schedule running a function after the sleep.
So instead of writing:
import { sleep } from "https://unpkg.com/ixfx/dist/flow.js"await sleep(100); // Pause for 100msawait someFn(); // Call and wait for someFn to run
You can write:
import { delay } from "https://unpkg.com/ixfx/dist/flow.js"await delay(someFn, 100);
sleep
is particularly elegant when you have multiple sleeps between code:
await doSomething();await sleep(100);await doSomethingElse();await sleep(50);await andAnotherThing();