Created
October 31, 2019 06:17
-
-
Save devarajchidambaram/da4d62c7eada13ee8d60687a0be94b4e to your computer and use it in GitHub Desktop.
Promises all
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function timerTask(){ | |
let [data1, data2] = await Promise.all([ | |
new Promise(function(resolve , reject){ | |
setTimeout(()=>{ | |
resolve('data1!') | |
}, 1000) | |
}), | |
new Promise(function(resolve , reject){ | |
setTimeout(()=>{ | |
resolve('data2!!') | |
}, 200) | |
}) | |
]) | |
// data.then(function(data){ | |
// console.log('data====', data) | |
// }) | |
console.log('data====>>>>>', data1, '======' , data2) | |
} | |
var ids = [1001, 1003, 1005, 1007, 1009, 1011, 1013] | |
let url = "http://dummy.restapiexample.com/api/v1/employee/" | |
//timerTask() | |
const axios = require('axios'); | |
async function getEmployeeDetails(ids){ | |
var requests = ids.map((id)=>{ | |
// return new Promise((resolve, reject)=>{ | |
// setTimeout(()=>{ | |
// console.log('----exec---' + id) | |
// resolve('id =>'+ id) | |
// }, id) | |
// }) | |
return axios.get(url + id) | |
}) | |
var p1 = await Promise.all(requests) | |
// p1.then(function(results){ | |
// results.forEach(result => console.log('data', result.data)) | |
// }) | |
p1.forEach(result => console.log('data', result.data)) | |
} | |
getEmployeeDetails(ids) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment