Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

promise.all async await

async function fetchABC() {
  const [a, b, c] = await Promise.all([a(), b(), c()]);

} 
Comment

async await promise all javascript

let characterResponse = await fetch('http://swapi.co/api/people/2/')
let characterResponseJson = await characterResponse.json()
let films = await Promise.all(
  characterResponseJson.films.map(async filmUrl => {
    let filmResponse = await fetch(filmUrl)
    return filmResponse.json()
  })
)
console.log(films)
Comment

Promise.all() with async and await

const runAsyncFunctions = async () => {
  const users = await getUsers()

  Promise.all(
    users.map(async (user) => {
      const userId = await getIdFromUser(user)
      console.log(userId)

      const capitalizedId = await capitalizeIds(userId)
      console.log(capitalizedId)
    })
  )

  console.log(users)
}
Comment

Promise.all() with async and await to run in console

// First promise returns an array after a delay
const getUsers = () => {
  return new Promise((resolve, reject) => {
    return setTimeout(
      () => resolve([{ id: 'ranjeet' }, { id: 'adil' }, { id: 'preet' }]),
      600
    )
  })
}


// Second promise relies on the result of first promise
const getIdFromUser = (user) => {
  return new Promise((resolve, reject) => {
    return setTimeout(() => resolve(user.id), 500)
  })
}


// Third promise relies on the result of the second promise
const capitalizeIds = (id) => {
  return new Promise((resolve, reject) => {
    return setTimeout(() => resolve(id.toUpperCase()), 200)
  })
}

const runAsyncFunctions = async () => {
  const users = await getUsers()

  Promise.all(
    users.map(async (user) => {
      const userId = await getIdFromUser(user)
      console.log(userId)

      const capitalizedId = await capitalizeIds(userId)
      console.log(capitalizedId)
    })
  )

  console.log(users)
}

runAsyncFunctions()
Comment

PREVIOUS NEXT
Code Example
Javascript :: array json 
Javascript :: lifecycle state: defunct, not mounted 
Javascript :: max value from array in javascript 
Javascript :: NODEJS ES6 STRING TO BASE64 
Javascript :: vscode shortcut to search for file 
Javascript :: conditional jsx property 
Javascript :: node.js web server 
Javascript :: bootstrap not working in print 
Javascript :: js contain character 
Javascript :: js array get index 
Javascript :: popup in browser js 
Javascript :: loop object array 
Javascript :: vue add external script 
Javascript :: media query in jsx 
Javascript :: networkx check if node exists 
Javascript :: lodash reduce 
Javascript :: $push in mongoose 
Javascript :: mongoose connect 
Javascript :: fatorial recursivo em javascript 
Javascript :: js instanceof 
Javascript :: how to add cdn link in shopify 
Javascript :: datatable index column server side 
Javascript :: jquery add class to body 
Javascript :: run function on page resize javascript 
Javascript :: js use restrict 
Javascript :: javascript regex stop at first match 
Javascript :: isprime js 
Javascript :: document fragment 
Javascript :: javascript convert number to spreadsheet column 
Javascript :: react alice carousel 
ADD CONTENT
Topic
Content
Source link
Name
2+8 =