如果其中一个属性需要承诺值,如何创建一个对象
How to create an object if one of it's properties requires a value of a 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}
仅仅将函数声明为 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 方法的更方便的方法。
这里有两个问题:
1) getTasaDolar
returns undefined
因为它没有明确地 return 任何东西
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
来查看 returned 值,因为 myFunc
是 async
因此 return 是 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);
如何使用承诺的值将其添加到对象 属性?如果有人能帮助我,我将不胜感激。
//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}
仅仅将函数声明为 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 方法的更方便的方法。
这里有两个问题:
1) getTasaDolar
returns undefined
因为它没有明确地 return 任何东西
2) 调用 getTasaDolar
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
来查看 returned 值,因为 myFunc
是 async
因此 return 是 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);