Promise.all Infers Tuple Result Types
Promise.all preserves the tuple shape of its input array, so destructured results keep per-element types instead of collapsing into one unioned array.
Code
type Weather = { celsius: number };
type News = { headlines: string[] };
const loadWeather = async (): Promise<Weather> => ({ celsius: 21.5 });
const loadNews = async (): Promise<News> => ({ headlines: ['deploy done', 'cache warm'] });
// Promise.all 保留元组形状:解构出的 weather 是 Weather,news 是 News
Promise.all([loadWeather(), loadNews()]).then(([weather, news]) => {
console.log('weather:', weather.celsius);
console.log('news:', news.headlines.join(' | '));
});
// 三元素元组同样成立:[string, number, boolean]
Promise.all([Promise.resolve('env'), Promise.resolve(2), Promise.resolve(true)]).then(
([name, version, ready]) => {
console.log(name, version, ready);
},
);Output
Captured from a sandboxed run of this snippet in the build pipeline.
weather: 21.5
news: deploy done | cache warm
env 2 trueHow it works
Promise.all Infers Tuple Result Types keeps everything in the functions loadWeather and loadNews.
Asynchronous boundaries use await, so failures surface as rejected promises at the call site instead of being swallowed.
Putting it to use
The primary signature is const loadWeather = async ()
The demo output is labeled (weather:, news:), so each printed line can be matched to the operation producing it.
Core lines
Promise.all([loadWeather(), loadNews()]).then(([weather, news]) => {
console.log('weather:', weather.celsius);
console.log('news:', news.headlines.join(' | '));
});
const loadNews = async (): Promise<News> => ({ headlines: ['deploy done', 'cache warm'] });
const loadWeather = async (): Promise<Weather> => ({ celsius: 21.5 });
FAQ
- Is anything beyond the standard library required?
- Nothing beyond the standard library — the snippet is dependency-free. The file defines `loadWeather`, `loadNews` at load time.
Report a problem with this exampleOpens a pre-filled issue on GitHub.