Будут работать либо ожидание, либо назначение в цепочке обещаний, в основном это зависит от того, как вам нужно использовать результаты обещания, какие переменные в области видимости и т. Д. Вот источник , который я нашел полезным при первом изучении тонкогоразличия между async/await
, Обещаниями и try/catch
, и тем, как они работают вместе.
async function post(orderData) {
const items = [];
for (let i in orderData){
//getting extra information about the products
var productData = await axios.get('/' + $(orderData[i].product_id));
items.push({
"product_code": orderData[i].product_id,
"name": orderData[i].product_name,
"cfop": productData.cfop, // no need to await here, you have the resolved value
"icms_situacao_tributaria": productData.icms,
...
});
...
}
или
async function post(orderData) {
const items = [];
for (let i in orderData){
await axios.get('/' + $(orderData[i].product_id));
.then(productData => {
items.push({
"product_code": orderData[i].product_id,
"name": orderData[i].product_name,
"cfop": productData.cfop, // no need to await here, you have the resolved value
"icms_situacao_tributaria": productData.icms,
...
});
});
...
}
или использованием карты
function post(orderData) {
return orderData.map(async (data) => {
return await axios.get('/' + $(data.product_id));
.then(productData => ({
"product_code": data.product_id,
"name": data.product_name,
"cfop": productData.cfop, // no need to await here, you have the resolved value
"icms_situacao_tributaria": productData.icms,
...
});
);
});
...
}