Call await in a callback inside an async function
const result = await [1, 2, 3].reduce(async function(prom, v){
const result= await prom;
await sleep(1000);
result.push(v);
return result;
}, Promise.resolve([]));
You could reduce to create a promise chain. However in your simplyfied case:
(a=b=>(b==2||(console.log("test"),setTimeout(a,1000,b+1))))(0);
If a library like bluebird is an option then you could write:
'use strict'
const Promise = require('bluebird')
async function test() {
return Promise.mapSeries([1, 2, 3], async (idx) => {
console.log('test: ' + idx);
await Promise.delay(1000)
});
}
test();