У меня есть несколько асинхронных вызовов, которые я хочу выполнить перед моим последним вызовом, и у меня есть метод, подобный этому stackoverflow-ответу .
Вот код в Codepen
class Person {
name: string;
constructor(init){
this.name = init;
}
}
let people: Person[] = [];
let names:string[] = ['janes','james','jo','john','josh'];
names.forEach(n=>people.push(new Person(n)));
function printName(name:string) {
let getSomething = new Promise(function(resolve, reject) {
setTimeout(function() {
resolve(name);
},1000)
});
getSomething.then(function(){
console.log(name);
});
}
/// main
let request = [];
console.log('start');
people.forEach(person => {
request.push(printName(person.name));
})
Promise.all(request).then(result=> {
console.log(result);
console.log("finsh");
})
Что произвел вышеуказанный код:
"start"
[undefined, undefined, undefined, undefined, undefined]
"finsh"
"janes"
"james"
"jo"
"john"
"josh"
в то время как я ожидаю:
"start"
"janes"
"james"
"jo"
"john"
"josh"
[undefined, undefined, undefined, undefined, undefined]
"finsh"