Giter Club home page Giter Club logo

Comments (5)

dejecj avatar dejecj commented on August 16, 2024

@coder-assist can you rewrite the sleep function in dejecj/sandbox/blob/main/index.js so that it doesn't use a Promise?

from sandbox.

coder-assist avatar coder-assist commented on August 16, 2024

Certainly! Here's an updated version of the sleep function from the dejecj/sandbox repository that does not use a Promise:

            function sleep(ms) {
                const start = Date.now();
                let currentTime = null;
                do {
                    currentTime = Date.now();
                } while (currentTime - start < ms);
            }

This function uses a do...while loop to block the JavaScript event loop and thereby pause the script's execution for the desired amount of time, rather than creating a Promise and invoking the setTimeout function.

from sandbox.

dejecj avatar dejecj commented on August 16, 2024

@coder-assits ok can you convert this to use promises?

function sleep(ms) {
                const start = Date.now();
                let currentTime = null;
                do {
                    currentTime = Date.now();
                } while (currentTime - start < ms);
            }

from sandbox.

dejecj avatar dejecj commented on August 16, 2024

@coder-assist ok can you convert this to use promises?

function sleep(ms) {
                const start = Date.now();
                let currentTime = null;
                do {
                    currentTime = Date.now();
                } while (currentTime - start < ms);
            }

from sandbox.

coder-assist avatar coder-assist commented on August 16, 2024

Sure, here's one way to convert the sleep function to use promises:

function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

This version of the sleep function creates a Promise instance that will resolve after a specified amount of time using the native setTimeout function. The resolve function passed to setTimeout is called after the specified ms duration, signaling that the promise has been fulfilled.

You can now use this promise version of sleep to handle asynchronous code in JavaScript.

from sandbox.

Related Issues (5)

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.