Код: Выделить всё
const resolveName = (fruitname) => {
return new Promise((resolve) => {
resolve(fruitname);
});
};
async function countLetters(fruitList) {
let result = "";
await Promise.all(
fruitList.map(async (fruit) => {
result += fruit + ": " + (await resolveName(fruit)).length + "\n";
// Fix: storing the string into a variable fixes this.
// const s = fruit + ": " + (await resolveName(fruit)).length + "\n";
// result += s;
})
);
return result;
}
const arr = ["apple", "banana", "cherry"];
const p = countLetters(arr);
p.then((r) => console.log(r));
< /code>
Я попытался отладки и просмотреть, так что для решения. Ожидаемый и фактический вывод равен ниже: < /p>
Ожидаемое:
apple: 5 banana: 6 cherry: 6 < /code> < /p>
Actual:
cherry: 6
Вот ссылка на выполнение, которая воспроизводит ошибку.
Подробнее здесь: https://stackoverflow.com/questions/794 ... -scoped-co