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 :: average javascript 
Javascript :: how to use object destructuring 
Javascript :: rxjs of 
Javascript :: d3 js 
Javascript :: null vs undefined 
Javascript :: javascript validator 
Javascript :: sequelize migration limit 
Javascript :: while loop javascript 
Javascript :: nodejs input 
Javascript :: reverse () method to reverse the array 
Javascript :: private routing in react 
Javascript :: Prerequisites before creating react-app 
Javascript :: return the sum of an array 
Javascript :: search in array javascript 
Javascript :: how to link js function to button 
Javascript :: js react 
Javascript :: Child nodes in a node 
Javascript :: how to add class in jquery 
Javascript :: AJAX in reload a div container 
Javascript :: javascript meme 
Javascript :: js map on object 
Javascript :: null check in javascript 
Javascript :: javascript if else 
Javascript :: loadsh debounce 
Javascript :: how to check if a number is negative in p5.js 
Javascript :: identifier in js 
Javascript :: une expression de fonction en javascript 
Javascript :: simple if condition for form validation 
Javascript :: file_get_contents api json 
Javascript :: js array Categorize New Member 
ADD CONTENT
Topic
Content
Source link
Name
2+5 =