Вот несколько способов исправить это:
function promiseFunc(number) {
return new Promise((resolve, reject) => {
if (number == 100) {
console.log('Inside Promise');
resolve('Done');
} else {
number += 1;
resolve(promiseFunc(number));
}
})
}
(async function testFunc() {
console.log('Starting');
await promiseFunc(0)
console.log("Test completed");
})()
Или, что интересно, эквивалентный код, который использует async
/ await
вместо явного new Promise(...)
, работает для вашего случая:
// an async function returns a Promise
async function promiseFunc(number) {
if (number == 100) {
console.log('Inside Promise');
return 'Done';
} else {
number += 1;
// it seems to work as intended without saying return promiseFunc(...)
promiseFunc(number);
}
}
(async function testFunc() {
console.log('Starting');
await promiseFunc(0)
console.log("Test completed");
})()