Simple sleep with async/await
npm install --save await-sleep
import * as sleep from 'await-sleep';
async function myAsyncFunction() {
console.time('Sleeping');
await sleep(1500);
console.timeEnd('Sleeping'); // Sleeping: 1507.180ms
}
Delay with async/await
Thanks for the nifty package!
I noticed in the README you describe it as a "blocking" sleep, but as far as I can tell, that isn't the case? It's kind of a nit-pick, but when it comes to sleeping in Node APIs it's a pretty important distinction. This will certainly block the function where it's used from continuing until the promise resolves, but the event loop will continue handling other requests. Here's a demo:
Non-blocking sleep
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
async function sleepThenLog() {
await sleep(5000);
console.log('slept for 5000ms');
}
async function immediatelyLog() {
console.log('no sleep at all');
}
sleepThenLog();
immediatelyLog();
Returns:
no sleep at all
...5000ms delay
slept for 5000ms
Blocking sleep (requires node 9.3+)
async function sleepThenLog() {
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 5000)
console.log('slept for 5000ms');
}
async function immediatelyLog() {
console.log('no sleep at all');
}
sleepThenLog();
immediatelyLog();
Returns (after 5000ms):
slept for 5000ms
no sleep at all
As in topic, package description mislead. Imo. this function is non-blocking:
https://github.com/sqren/await-sleep/blob/3b4ba9421df46b081e770bf832320c1bbcf33e0e/package.json#L4
I'm currently using this and when I deploy to AWS it gives me this response. Currently working in an asynchronous typescript environment.
TypeError: await_sleep_1.default is not a function at Object.<anonymous> (/var/task/src/service/Customer.js:41:36) at next (native) at fulfilled (/var/task/src/service/Customer.js:4:58) at process._tickDomainCallback (internal/process/next_tick.js:135:7) }
It's imported like so import sleep from 'await-sleep'
and put in my service function like so await sleep(1500);
thanks!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.