const convert = async(value) => setTimeout(() => value.split('').reverse().join(''), 100)
const data = {
result: [
{
"test 1": [
{ number : "12356789"},
{ number : "12356789"}
],
"test 2": [
{number: "12356789"}
],
"test 3": [
{number: "12356789"}
]
},
{
total: {
records: 3
}
}
]
}
How do i convert each number value using async convert function ?
Tried with forEach with multiple loop, but did not work. So logic should be synchronies. any suggestion much appreciate.
2
Answers
You need to loop over your
data
element and call your methodconvert
once you reach thenumber
convert(myNumber)
You should return a promise from your convert function
You should use
await
to wait for the convert function to return new valuesUse
Promise.all()
and map your numbers to array of promises: