如何在一行中显示 2 个承诺的价值?

How to show value from 2 promises in a single line?

我有一个 class 静态方法返回承诺。我能够在控制台中以单独的行打印这些承诺返回的值。但我想在控制台中 一行 打印给定城市(比如伦敦)的天气和货币。 输出应该是这样的:

 The weather of London is cloudy. The local currency of London is GBP.

我也无法用嵌套的承诺来做到这一点。应该怎么做?

代码如下:

    class Provider{
            static getWeather(city){
            return Promise.resolve(`The weather of ${city} is cloudy.`);
            }
            static getLocalCurrency(city){
            return Promise.resolve(`The local currency of ${city} is GBP`)
            }
         };

    Provider.getWeather(value).then((value)=> console.log(value));
    Provider.getLocalCurrency("London").then((value)=> console.log(value));   


    

您可以使用 Promise.all.

class Provider {
  static getWeather(city) {
    return Promise.resolve(`The weather of ${city} is cloudy.`);
  }
  static getLocalCurrency(city) {
    return Promise.resolve(`The local currency of ${city} is GBP`);
  }
}

Promise.all([
  Provider.getWeather("london"),
  Provider.getLocalCurrency("London")
]).then(([weather, currency]) => {
  console.log(`${weather} ${currency}`);
});