async function main() {
console.log('begin');
await asyncWork(10000);
console.log('end');
}
function asyncWork(ms) {
var i = 0;
return new Promise(resolve => {
const foo = () => {
if (i > 99) {
resolve();
} else {
i++;
console.log(`${i}% is done`);
setTimeout(foo, ms / 100);
}
}
foo();
});
}
main().then(() => console.log('main is done'));