Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

async function in variable

function resolveAfter2Seconds(x) {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve(x);
    }, 2000);
  });
};

(async function(x) { // fonction asynchrone immédiatement appelée
  var a = resolveAfter2Seconds(20);
  var b = resolveAfter2Seconds(30);
  return x + await a + await b;
})(10).then(v => {
  console.log(v);  // affiche 60 après 2 secondes.
});

var add = async function(x) {
  var a = await resolveAfter2Seconds(20);
  var b = await resolveAfter2Seconds(30);
  return x + a + b;
};

add(10).then(v => {
  console.log(v);  // affiche 60 après 4 secondes.
});
Comment

PREVIOUS NEXT
Code Example
Javascript :: maximum number in javascript 
Javascript :: javascript reflection 
Javascript :: react click outside class implementation 
Javascript :: show modal by using id in list react 
Javascript :: search an array with regex javascript indexOf 
Javascript :: how to clone an object in javascript 
Javascript :: last element of array 
Javascript :: nodejs global 
Javascript :: how to sum the array values in javascript 
Javascript :: functional component react 
Javascript :: id button click jquery 
Javascript :: javascript hoisting 
Javascript :: dispay react component after some time 
Javascript :: javascript span style 
Javascript :: angular 8 enable routing 
Javascript :: searc and replace fcc solution 
Javascript :: loop react components 
Javascript :: object comparison in javascript 
Javascript :: footer bottom 
Javascript :: iterate over array javascript 
Javascript :: js map 
Javascript :: Check if instance is array 
Javascript :: swift encode json 
Javascript :: generate qr code react 
Javascript :: js objects 
Javascript :: add active in nav 
Javascript :: add column sequelize 
Javascript :: hoisting in javascript 
Javascript :: javascript pop object from array 
Javascript :: javascript check negative number 
ADD CONTENT
Topic
Content
Source link
Name
6+6 =