javascript async delay
await new Promise(r => setTimeout(r, 2000));async function init() {
console.log(1);
await sleep(1000);
console.log(2);
}
function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
// one liner
let sleep = ms => new Promise(resolve => setTimeout(resolve, ms));const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
(async () => {
console.log('1');
await delay(1000);
console.log('2');
})();//Javascipt is asyncronous so you can't pause/block code execution
//You can delay doing something with setTimeout
setTimeout(function(){
alert("Sup!");
}, 2000);//wait 2 seconds
Also in JavaScript:
- convert table to excel reactjs
- laravel return response json
- http header express
- how to uninstall nodejs web server
- post method javascript code
- Find the stray number
- get current week number javascript
- array data structure javascipt interview questions
- javascript wait 5 sec
- how to print array backwards
- jquery empty ul
- array object inside an arrar in vue
- add class jquery
- javascript get width of a div
- how to get all elements with same class in javascript
- angular [routerlink]
- how to right plain text format file in node js
- checking if a character is an alphabet in js
- parsing through json without using key value python
- anagram program in javascript
- js get object by id from array
- mysql json_array_append
- how to learn javascript
- how to set JSON data bootstrap treeview with java