Git Product home page Git Product logo

await-sleep's Introduction

await-sleep

Simple sleep with async/await

Installation

npm install --save await-sleep

Usage

import * as sleep from 'await-sleep';

async function myAsyncFunction() {
  console.time('Sleeping');

  await sleep(1500);

  console.timeEnd('Sleeping'); // Sleeping: 1507.180ms
}

await-sleep's People

Contributors

sorenlouv avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

await-sleep's Issues

Blocking vs non-blocking

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

AWS Lambda TypeError:

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!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.