“JavaScript Async Delay” Code-Antworten

JavaScript Async Delay

const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));

(async () => {
  console.log('1');
  await delay(1000);
  console.log('2');
})();
garzj

Knoten warten 10 Sekunden

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));
just-saved-you-a-stackoverflow-visit

JavaScript -Version des Schlafes

//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
Grepper

Knoten JS asynchronisierter Verzögerung

const { promisify } = require('util');

const delay = promisify(setTimeout);

// Usage:
(async () => {
  console.log('1');
  await delay(1000);
  console.log('2');
})();
garzj

Ähnliche Antworten wie “JavaScript Async Delay”

Fragen ähnlich wie “JavaScript Async Delay”

Weitere verwandte Antworten zu “JavaScript Async Delay” auf JavaScript

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen