Comments (5)
@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.
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.
@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.
@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.
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)
- Bug hunt HOT 22
- Code Analysis HOT 52
- Test HOT 27
- Test HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sandbox.