mirror of
https://github.com/jbranchaud/til
synced 2026-01-04 23:58:01 +00:00
Add Sleep For A Bit In Async Code as a JavaScript til
This commit is contained in:
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
|
|||||||
|
|
||||||
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
|
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
|
||||||
|
|
||||||
_1159 TILs and counting..._
|
_1160 TILs and counting..._
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -413,6 +413,7 @@ _1159 TILs and counting..._
|
|||||||
- [Resolve And Pass Multiple Values From A Then](javascript/resolve-and-pass-multiple-values-from-a-then.md)
|
- [Resolve And Pass Multiple Values From A Then](javascript/resolve-and-pass-multiple-values-from-a-then.md)
|
||||||
- [Running ES6 Specs With Mocha](javascript/running-es6-specs-with-mocha.md)
|
- [Running ES6 Specs With Mocha](javascript/running-es6-specs-with-mocha.md)
|
||||||
- [Scoping Variables With A Block Statement](javascript/scoping-variables-with-a-block-statement.md)
|
- [Scoping Variables With A Block Statement](javascript/scoping-variables-with-a-block-statement.md)
|
||||||
|
- [Sleep For A Bit In Async Code](javascript/sleep-for-a-bit-in-async-code.md)
|
||||||
- [Sorting Arrays Of Objects With Lodash](javascript/sorting-arrays-of-objects-with-lodash.md)
|
- [Sorting Arrays Of Objects With Lodash](javascript/sorting-arrays-of-objects-with-lodash.md)
|
||||||
- [Splat Arguments To A Function](javascript/splat-arguments-to-a-function.md)
|
- [Splat Arguments To A Function](javascript/splat-arguments-to-a-function.md)
|
||||||
- [Spread The Rest With ES6](javascript/spread-the-rest-with-es6.md)
|
- [Spread The Rest With ES6](javascript/spread-the-rest-with-es6.md)
|
||||||
|
|||||||
34
javascript/sleep-for-a-bit-in-async-code.md
Normal file
34
javascript/sleep-for-a-bit-in-async-code.md
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
# Sleep For A Bit In Async Code
|
||||||
|
|
||||||
|
A `sleep` utility function can be useful in a variety of situations. From
|
||||||
|
testing and debugging to simulating a delay in development.
|
||||||
|
|
||||||
|
Here is what that function can look like in its simplest form:
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
function sleep(time) {
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
setTimeout(resolve, time)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
This function returns a promise that will resolve after the given number of
|
||||||
|
milliseconds.
|
||||||
|
|
||||||
|
As an example of how to use it, here is how we can simulate a delay in a fake
|
||||||
|
fetch function.
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
async function fakeUserFetch(userId) {
|
||||||
|
# add half a second of "network" latency
|
||||||
|
await sleep(500)
|
||||||
|
|
||||||
|
const fakeResponse = {
|
||||||
|
id: userId,
|
||||||
|
email: "fake-email@example.com"
|
||||||
|
}
|
||||||
|
|
||||||
|
return Promise.resolve(fakeResponse)
|
||||||
|
}
|
||||||
|
```
|
||||||
Reference in New Issue
Block a user