I'm struggling with .map loop and async function inside. I use request-promise for async requests.
import * as rp from 'request-promise';
const testArray = ['one', 'two', 'three'];
const link = 'https://somelink.com/';
const test = testArray.map(async (elem) => {
console.log('before', elem);
await rp.get(link)
.then(async () => {
console.log('success');
});
console.log('after', elem);
});
Promise.all(test);
The output of this code:
before one
before two
before three
success
after one
success
after three
success
after two
What I need is that the code to execute in the correct order with output like this:
before one
success
after one
before two
success
after two
before three
success
after three
Can't figure out what am I doing wrong. Please help.