【问题标题】:How to create an object if one of it's properties requires a value of a promise如果一个对象的属性需要一个承诺值,如何创建一个对象
【发布时间】:2020-05-13 23:14:26
【问题描述】:

如何使用 promise 的值将其添加到对象属性?如果有人可以帮助我,将不胜感激。

//dolar.js

const axios = require('axios');
const url ='https://s3.amazonaws.com/dolartoday/data.json';

async function getTasaDolar(priceBs) {
  axios.get(url).then(function (response) {
    return response.data.USD.transferencia;
  }).catch(function(error) {
  }).then(function(response) {{
    console.log('--------');
    console.log(priceBs/response);
    return priceBs/response;
  }});
}

module.exports = getTasaDolar;

//object.js

const getTasaDolar = require('./dolar.js');

async function myFunc ({name, brand, category, priceBs}) {
    const object =  {
        name,
        brand,
        category,
        priceBs,
        priceDolar :  getTasaDolar(priceBs)
    }

    return object;
}

const hola =  myFunc({name:`hola`, brand: `Chocozuela`, category:`Carnes`, priceBs:645000});
console.log(hola);

当我运行它时,输出是:

Promise {
  { name: 'hola',
    brand: 'Chocozuela',
    category: 'Carnes',
    priceBs: 645000,
    priceDolar: Promise { undefined } } }
--------
3.467229680260142.

如您所见,值 3.46 是 consoled.log 但是当我 console.log hola 对象时,属性 priceDolar 是 Promise {undefined}

【问题讨论】:

  • getTasaDolar 函数不返回任何东西 - 试试return axios.get(url)........ - 因为它是一个异步函数,它肯定会返回一个承诺......所以你需要await getTasaDolar
  • 为什么要使用两个 then 块?
  • getTasaDolar 也是async 无缘无故
  • 我尝试格式化您的代码,使其更具可读性,但那些双括号是怎么回事,即.then(function(response) {{ ????
  • @Phil - 这是有效代码:p

标签: javascript object asynchronous promise response


【解决方案1】:

仅仅将函数声明为async 是不够的;你仍然需要await Promise 才能使用它们的结果。

第一个问题是getTasaDolar 没有等待任何东西。修复:

async function getTasaDolar(priceBs) {
  const response = await axios.get(url);
  return priceBs / response.data.USD.transferencia;
}

现在您还必须在myFunc 中等待:

async function myFunc ({name, brand, category, priceBs}) {
  return {
    name,
    brand,
    category,
    priceBs,
    priceDolar: await getTasaDolar(priceBs)
  };
}

是的,异步性也上升到myFunc,所以如果你想调用它,你必须使用await 或 Promise 回调:

myFunc({name:`hola`, brand: `Chocozuela`, category:`Carnes`, priceBs:645000})
  .then(hola => console.log(hola));

也许你有一个常见的误解,即创建一个函数 async 会告诉 JavaScript 自动等待异步事件,但它真正意味着你可以使用关键字 await ,这是调用.then(...)、.catch(...) 和.finally(...) Promise 方法的更方便的方法。

【讨论】:

  • 我几乎可以看到后续问题,myFunc 正在返回一个承诺:p
【解决方案2】:

这里有两个问题: 1) getTasaDolar 返回 undefined 因为它没有明确返回任何内容 2) 调用getTasaDolar时不需要等待Promise 解决

getTasaDolar 不必是async,因为它不使用await。也不需要catch,因为调用它的人都可以处理。您正在消耗会导致响应未定义的错误。以下是我如何调整此功能:

function getTasaDolar(priceBs) {
  return axios
    .get(url)
    .then(function(response) {
      const data = response.data.USD.transferencia;
      console.log("--------");
      console.log(priceBs / data);
      return priceBs / data;
    });
}

myFunc 函数最好是 async,你应该 await 第一个函数的结果。但是在调用myFunc 的地方必须使用.then 来查看返回值,因为myFunc 是async,因此返回一个Promise 对象。

async function myFunc({ name, brand, category, priceBs }) {
  const object = {
    name,
    brand,
    category,
    priceBs,
    priceDolar: await getTasaDolar(priceBs) // <-- await here
  };

  return object;
}

myFunc({name:`hola`, brand: `Chocozuela`, category:`Carnes`, 
        priceBs:645000}).then(hola => console.log(hola);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-16
    • 2016-07-13
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 2019-04-27
    • 2011-01-26
    相关资源
    最近更新 更多