JSFiddle - React, Tailwind, and code Playground
by Chetan Hanumantha
JavaScript
var employees = [
{
"name": "abc",
"age": 28,
"dept": "analytics"
},
{
"name": "pqr",
"age": 25,
"dept": "finance"
},
{
"name": "xyz",
"age": 31,
"dept": "admin"
},
{
"name": "xxx",
"age": 35,
"dept": "analytics"
},
{
"name": "def",
"age": 32,
"dept": "HR"
},
{
"name": "fgh",
"age": 33,
"dept": "dev"
}
]
// Modify the above array of objects, and add a strVal property
// like 1st object should become:
/*
{
"Name": "abc",
"Age": 28,
"dept": "analytics",
"strVal": //api call
}
similar property strVal get added to all the objects
*/
for(var i=0; i<employees.length; i++){
// call makeApiCall and add new strVal property
function outerFn(index) {
var promises[] = makeApiCall().then(function(res){
console.log('Response: '+res+', index value: '+index);
employees[index].strVal = res;
}, function(err){
console.log('some error: '+err);
});
}
outerFn(i);
}
Promise.all(promisees).then()
Promise.any(arrPromies).then()
console.log(employees);
const str1 = "RANDOMSTRING";
function makeApiCall(){
return new Promise(function(resolve, reject) {
setTimeout(function(){
resolve(str1.substring(Math.random()*str1.length));
}, Math.random()*10);
})
}