Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Async functions and execution order

function resolveAfter2Seconds() {
  console.log("starting slow promise")
  return new Promise(resolve => {
    setTimeout(function() {
      resolve("slow")
      console.log("slow promise is done")
    }, 2000)
  })
}

function resolveAfter1Second() {
  console.log("starting fast promise")
  return new Promise(resolve => {
    setTimeout(function() {
      resolve("fast")
      console.log("fast promise is done")
    }, 1000)
  })
}

async function sequentialStart() {
  console.log('==SEQUENTIAL START==')

  // 1. Execution gets here almost instantly
  const slow = await resolveAfter2Seconds()
  console.log(slow) // 2. this runs 2 seconds after 1.

  const fast = await resolveAfter1Second()
  console.log(fast) // 3. this runs 3 seconds after 1.
}

async function concurrentStart() {
  console.log('==CONCURRENT START with await==');
  const slow = resolveAfter2Seconds() // starts timer immediately
  const fast = resolveAfter1Second() // starts timer immediately

  // 1. Execution gets here almost instantly
  console.log(await slow) // 2. this runs 2 seconds after 1.
  console.log(await fast) // 3. this runs 2 seconds after 1., immediately after 2., since fast is already resolved
}

function concurrentPromise() {
  console.log('==CONCURRENT START with Promise.all==')
  return Promise.all([resolveAfter2Seconds(), resolveAfter1Second()]).then((messages) => {
    console.log(messages[0]) // slow
    console.log(messages[1]) // fast
  })
}

async function parallel() {
  console.log('==PARALLEL with await Promise.all==')

  // Start 2 "jobs" in parallel and wait for both of them to complete
  await Promise.all([
      (async()=>console.log(await resolveAfter2Seconds()))(),
      (async()=>console.log(await resolveAfter1Second()))()
  ])
}

sequentialStart() // after 2 seconds, logs "slow", then after 1 more second, "fast"

// wait above to finish
setTimeout(concurrentStart, 4000) // after 2 seconds, logs "slow" and then "fast"

// wait again
setTimeout(concurrentPromise, 7000) // same as concurrentStart

// wait again
setTimeout(parallel, 10000) // truly parallel: after 1 second, logs "fast", then after 1 more second, "slow"
Comment

PREVIOUS NEXT
Code Example
Javascript :: format file using jq input curl 
Javascript :: invert binary tree js 
Javascript :: Imports should be sorted alphabetically sort-imports 
Javascript :: javascript responsive carousel 
Javascript :: Storing Values With Assignment Operators 
Javascript :: javascript python like for loop 
Javascript :: send offer webrtc 
Javascript :: telegram web app js 
Javascript :: javascript dropdown options auto-updating 
Javascript :: random number between 0 and 50 then round to a whole number 
Javascript :: js shufflin 
Javascript :: how to pass jsp variable as parameter via onclick function in html 
Javascript :: fs keep file open 
Javascript :: js tabbed data to array 
Javascript :: CalendarTriggerBuilder 
Javascript :: clasp enable oauthScopes appsscript.json 
Javascript :: change frame rate javascript 
Javascript :: storing jason format in perl and retriving it 
Javascript :: axios get request body 
Javascript :: js console 
Javascript :: addclass array 
Javascript :: get members of a group graph pnp js 
Javascript :: geojson polygon mongoose 
Javascript :: javascript to send email on button click 
Javascript :: conditional json spread operator 
Javascript :: javascript array filter exercises 
Javascript :: node.js vds connection was aborted 
Javascript :: add operator in javascript 
Javascript :: change to kebabcase in javascript 
Javascript :: navigate between files in react js 
ADD CONTENT
Topic
Content
Source link
Name
1+3 =