“retard asynchrone javascript” Réponses codées

retard asynchrone javascript

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

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

Node attendez 10 secondes

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

Version javascript du sommeil

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

Node JS Délai asynchrone

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

const delay = promisify(setTimeout);

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

Réponses similaires à “retard asynchrone javascript”

Questions similaires à “retard asynchrone javascript”

Plus de réponses similaires à “retard asynchrone javascript” dans JavaScript

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code