Coder Social home page Coder Social logo

sleep's Introduction

@awaitbox/sleep

Await for a certain amount of time.

npm i @awaitbox/sleep --save

The sleep async function returns a promise that will resolve after the specified amount of time. This is a convenient alternative for setTimeout. This is inspired from sleep(...) in the C language, except that time is specified in milliseconds rather than seconds.

You can use it in async functions:

import sleep from '@awaitbox/sleep'

async function main() {
  await sleep( 1000 )
  console.log( 'I fell asleep for a second!' )
}

main()

You can of course use it as a Promise:

import sleep from '@awaitbox/sleep'

sleep( 1000 )
  .then( data => console.log( 'I took a 1-second nap!' ) )

Chain values will pass through if you use it in a Promise chain like follows:

import sleep from '@awaitbox/sleep'

fetch( ... )
  .then( ... )
  .then( sleep.bind( null, 1000 ) ) // passes data through after 1 second
  .then( data => console.log( 'use data for the awesome!', data ) )

Note

This is written in ES2016+ JavaScript. To use this in pre-ES2016 environments, you'll need to run this through a transpiler like Babel (and I recommend using the fast-async plugin to get the best results). See some tips here on wiring it up with Webpack: http://2ality.com/2017/06/pkg-esnext.html.

sleep's People

Contributors

trusktr avatar

Watchers

 avatar

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.