Async Programming
Promise All
Promise.all waits for several promises and returns their values together.
Promise.all
`Promise.all` receives an array of promises and resolves when every promise resolves.
Promise All
all.ts
Replay: real traced execution (multi-file project)
async function loadLabel(name: string): Promise<string> {
return Promise.resolve(name.toUpperCase());
}
const secondName: string = "script";
async function main(): Promise<void> {
const labels: string[] = await Promise.all([
loadLabel("type"),
loadLabel(secondName),
]);
console.log(labels.join(","));
}
main();
async function loadLabel(name: string): Promise<string> {
return Promise.resolve(name.toUpperCase());
}
const secondName: string = "types";
async function main(): Promise<void> {
const labels: string[] = await Promise.all([
loadLabel("type"),
loadLabel(secondName),
]);
console.log(labels.join(","));
}
main();
async function loadLabel(name: string): Promise<string> {
return Promise.resolve(name.toUpperCase());
}
const secondName: string = "async";
async function main(): Promise<void> {
const labels: string[] = await Promise.all([
loadLabel("type"),
loadLabel(secondName),
]);
console.log(labels.join(","));
}
main();
secondName ← script
2 return Promise.resolve(name.toUpperCase());3}45const secondName→ script: string = "script"; //@secondName="types", "async"67async function main(): Promise<void> {8 const labels: string[] = await Promise.all([9 loadLabel("type"),10 loadLabel(secondName),11 ]);12 console.log(labels.join(","));13}1415main();//@secondName="types", "async" async function main(): Promise<void>
5const secondName: string = "script"; //@secondName="types", "async"67async function main(): Promise<void> {8 const labels: string[] = await Promise.all([9 loadLabel("type"),10 loadLabel(secondNamescript),11 ]);12 console.log(labels.join(","));async function loadLabel(name: string): Promise<string>
pass 1 of 21async function loadLabel(nametype: string): Promise<string> {2 return Promise.resolve(nametype.toUpperCase());3}async function loadLabel(name: string): Promise<string>
pass 2 of 21async function loadLabel(namescript: string): Promise<string> {2 return Promise.resolve(namescript.toUpperCase());3}labels ← TYPE,SCRIPT
7async function main(): Promise<void> {8 const labels→ TYPE,SCRIPT: string[] = await Promise.all([9 loadLabel("type"),10 loadLabel(secondNamescript),11 ]);12 console.log(labelsTYPE,SCRIPT.join(","));13}1415main();outputTYPE,SCRIPT
secondName ← types
2 return Promise.resolve(name.toUpperCase());3}45const secondName→ types: string = "types";67async function main(): Promise<void> {8 const labels: string[] = await Promise.all([9 loadLabel("type"),10 loadLabel(secondName),11 ]);12 console.log(labels.join(","));13}1415main();async function main(): Promise<void>
5const secondName: string = "types";67async function main(): Promise<void> {8 const labels: string[] = await Promise.all([9 loadLabel("type"),10 loadLabel(secondNametypes),11 ]);12 console.log(labels.join(","));async function loadLabel(name: string): Promise<string>
pass 1 of 21async function loadLabel(nametype: string): Promise<string> {2 return Promise.resolve(nametype.toUpperCase());3}async function loadLabel(name: string): Promise<string>
pass 2 of 21async function loadLabel(nametypes: string): Promise<string> {2 return Promise.resolve(nametypes.toUpperCase());3}labels ← TYPE,TYPES
7async function main(): Promise<void> {8 const labels→ TYPE,TYPES: string[] = await Promise.all([9 loadLabel("type"),10 loadLabel(secondNametypes),11 ]);12 console.log(labelsTYPE,TYPES.join(","));13}1415main();outputTYPE,TYPES
secondName ← async
2 return Promise.resolve(name.toUpperCase());3}45const secondName→ async: string = "async";67async function main(): Promise<void> {8 const labels: string[] = await Promise.all([9 loadLabel("type"),10 loadLabel(secondName),11 ]);12 console.log(labels.join(","));13}1415main();async function main(): Promise<void>
5const secondName: string = "async";67async function main(): Promise<void> {8 const labels: string[] = await Promise.all([9 loadLabel("type"),10 loadLabel(secondNameasync),11 ]);12 console.log(labels.join(","));async function loadLabel(name: string): Promise<string>
pass 1 of 21async function loadLabel(nametype: string): Promise<string> {2 return Promise.resolve(nametype.toUpperCase());3}async function loadLabel(name: string): Promise<string>
pass 2 of 21async function loadLabel(nameasync: string): Promise<string> {2 return Promise.resolve(nameasync.toUpperCase());3}labels ← TYPE,ASYNC
7async function main(): Promise<void> {8 const labels→ TYPE,ASYNC: string[] = await Promise.all([9 loadLabel("type"),10 loadLabel(secondNameasync),11 ]);12 console.log(labelsTYPE,ASYNC.join(","));13}1415main();outputTYPE,ASYNC