Tengo una clase con métodos estáticos que devuelven promesas. Puedo imprimir en la consola los valores devueltos por estas promesas en líneas separadas. Pero quiero imprimir en la consola en una sola línea el clima y la moneda de una ciudad determinada (por ejemplo, Londres). La salida debería ser así:
The weather of London is cloudy. The local currency of London is GBP.No puedo hacerlo con promesas anidadas también. ¿Cómo se debe hacer?
Aquí está el código:
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));Puedes usar 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}`); });