我正在使用Zapier代码(Javascript)调用第三方API来转换货币。每次我按下Zap时,它将进行约40次API调用。
我正在使用setTimeout进行延迟,并确保这些值可用并且API调用已完成,然后再通过Zapier的输出变量将其发送到下一步,但似乎它们被忽略了...整个zapier操作将在384毫秒内执行。
我与Zapier进行了交谈,并被告知他们确实支持setTimeout。我究竟做错了什么?我无法弄清楚...:)
**在下面的答案中更新
//This code runs API calls for all Amount, OOP and GM currency conversions.
//* Note - Timeout is used to allow the API calls to finish and ensure those results are set and available before setting the final Zapier output values.
var fromAmt;
var fromOOP;
var fromGM;
var fromCur = inputData.currency;
//set input values from Zapier and replace with "0" if null
if (inputData.amount == null){fromAmt = 0;}
if (inputData.outofpocket == null){fromOOP = 0;}
if (inputData.gm == null){fromGM = 0;}
var toCur = ["USD", "EUR", "INR", "GBP", "SGD", "AUD", "CNY", "HKD", "ARS", "AED", "MXN", "NZD", "BRL", "CAD", "SEK", "COP"];
var dAmt = {"USD":10,"EUR":0,"INR":0,"GBP":0,"SGD":0,"AUD":0, "CNY":0, "HKD":0, "ARS":0, "AED":0, "MXN":0, "NZD":0, "BRL":0, "CAD":0, "SEK":0, "COP":0};
var dOOP = {"USD":0,"EUR":0,"INR":0,"GBP":0,"SGD":0,"AUD":0, "CNY":0, "HKD":0, "ARS":0, "AED":0, "MXN":0, "NZD":0, "BRL":0, "CAD":0, "SEK":0, "COP":0};
var dGM = {"USD":0,"EUR":0,"INR":0,"GBP":0,"SGD":0,"AUD":0, "CNY":0, "HKD":0, "ARS":0, "AED":0, "MXN":0, "NZD":0, "BRL":0, "CAD":0, "SEK":0, "COP":0};
output = [{amtUSD: 0,
amtEUR: 0,
amtINR: 0,
amtGBP: 0,
amtSGD: 0,
amtAUD: 0,
amtCNY: 0,
amtHKD: 0,
amtARS: 0,
amtAED: 0,
amtMXN: 0,
amtNZD: 0,
amtBRL: 0,
amtCAD: 0,
amtSEK: 0,
amtCOP: 0,
oopUSD: 0,
oopEUR: 0,
oopINR: 0,
oopGBP: 0,
oopSGD: 0,
oopAUD: 0,
oopCNY: 0,
oopHKD: 0,
oopARS: 0,
oopAED: 0,
oopMXN: 0,
oopNZD: 0,
oopBRL: 0,
oopCAD: 0,
oopSEK: 0,
oopCOP: 0,
gmUSD: 0,
gmEUR: 0,
gmINR: 0,
gmGBP: 0,
gmSGD: 0,
gmAUD: 0,
gmCNY: 0,
gmHKD: 0,
gmARS: 0,
gmAED: 0,
gmMXN: 0,
gmNZD: 0,
gmBRL: 0,
gmCAD: 0,
gmSEK: 0,
gmCOP: 0,}];
function setOutput()
{
output["amtUSD"] = dAmt["USD"];
output["amtEUR"] = dAmt["EUR"];
output["amtINR"] = dAmt["INR"];
output["amtGBP"] = dAmt["GBP"];
output["amtSGD"] = dAmt["SGD"];
output["amtAUD"] = dAmt["AUD"];
output["amtCNY"] = dAmt["CNY"];
output["amtHKD"] = dAmt["HKD"];
output["amtARS"] = dAmt["ARS"];
output["amtAED"] = dAmt["AED"];
output["amtMXN"] = dAmt["MXN"];
output["amtNZD"] = dAmt["NZD"];
output["amtBRL"] = dAmt["BRL"];
output["amtCAD"] = dAmt["CAD"];
output["amtSEK"] = dAmt["SEK"];
output["amtCOP"] = dAmt["COP"];
output["oopUSD"] = dOOP["USD"];
output["oopEUR"] = dOOP["EUR"];
output["oopINR"] = dOOP["INR"];
output["oopGBP"] = dOOP["GBP"];
output["oopSGD"] = dOOP["SGD"];
output["oopAUD"] = dOOP["AUD"];
output["oopCNY"] = dOOP["CNY"];
output["oopHKD"] = dOOP["HKD"];
output["oopARS"] = dOOP["ARS"];
output["oopAED"] = dOOP["AED"];
output["oopMXN"] = dOOP["MXN"];
output["oopNZD"] = dOOP["NZD"];
output["oopBRL"] = dOOP["BRL"];
output["oopCAD"] = dOOP["CAD"];
output["oopSEK"] = dOOP["SEK"];
output["oopCOP"] = dOOP["COP"];
output["gmUSD"] = dGM["USD"];
output["gmEUR"] = dGM["EUR"];
output["gmINR"] = dGM["INR"];
output["gmGBP"] = dGM["GBP"];
output["gmSGD"] = dGM["SGD"];
output["gmAUD"] = dGM["AUD"];
output["gmCNY"] = dGM["CNY"];
output["gmHKD"] = dGM["HKD"];
output["gmARS"] = dGM["ARS"];
output["gmAED"] = dGM["AED"];
output["gmMXN"] = dGM["MXN"];
output["gmNZD"] = dGM["NZD"];
output["gmBRL"] = dGM["BRL"];
output["gmCAD"] = dGM["CAD"];
output["gmSEK"] = dGM["SEK"];
output["gmCOP"] = dGM["COP"];
}
async function getConversionAsync(type, fcur, tcur, amt) {
let response = await fetch('https://data.fixer.io/api/convert?access_key=xxx&from=' + fcur + '&to=' + tcur + '&amount=' + amt);
let data = await response.json();
if (type == "Amount"){
dAmt[tcur] = await parseFloat(data.result.toString()).toFixed(2);
}
else if (type == "OOP"){
dOOP[tcur] = await parseFloat(data.result.toString()).toFixed(2);
}
else if (type == "GM"){
dGM[tcur] = await parseFloat(data.result.toString()).toFixed(2);
}
}
function getConvertedAmounts(fAmt,fOOP,fGM,cur){
var i;
for (i=0; i < toCur.length; i++){
getConversionAsync("Amount", cur,toCur[i],fAmt);
}
for (i=0; i < toCur.length; i++){
getConversionAsync("OOP", cur,toCur[i],fOOP);
}
for (i=0; i < toCur.length; i++){
getConversionAsync("GM", cur,toCur[i],fGM);
}
}
getConvertedAmounts(fromAmt,fromOOP,fromGM,fromCur);
//In Zapier this is where we write the values out to the next function
setTimeout(function(){ setOutput(); }, 5000);
答案 0 :(得分:0)
根本问题是setTimeout
的运行不像您期望的那样。尝试在JS解释器中运行它:
const st = () => {
setTimeout(()=>console.log('done'), 3000)
return 3
}
st()
3
立即打印,然后3秒钟(如果解释器仍处于活动状态,则打印'done'
。这是因为setTimeout
不会阻止执行-它只是排队n
秒后执行一个操作(回调结果)。this答案中有更多信息。
要记住的一大事情是Zapier并没有在代码中做任何特别神奇的事情。有一个包装器可以处理lambda,就是这样。因此,如果将现有代码复制到函数中并运行它,则应该看到Zapier会得到的相同内容-执行立即完成,并且不返回任何内容。
要解决此问题,您将需要await
个getConversionAsync
电话。但是,您执行Zapier的时间有限,因此您希望并行执行尽可能多的请求。
为此,做出大量承诺并将它们全部包装在Promise.all
中。我不太肯定您不会用完时间,但这至少会让您朝正确的方向前进。
答案 1 :(得分:0)
我尝试了上述有针对性的想法,并删除了所有可重用性。现在,只有一堆带有等待行的语句-据我所知-将按顺序运行它们吗?
无论如何,动作在200毫秒内完成,这意味着它甚至都没有等待。
当我在Zapier以外的常规环境中运行此代码时,它可以很好地完成-大约需要5秒钟来运行所有内容。
我还想念什么吗?假设我还是不明白哈哈:)
//* Note - Timeout is used to allow the API calls to finish and ensure those results are set and available before setting the final Zapier output values.
var fromAmt;
var fromOOP;
var fromGM;
var fromCur = inputData.currency;
//set input values from Zapier and replace with "0" if null
if (inputData.amount == null){fromAmt = 0;}
if (inputData.outofpocket == null){fromOOP = 0;}
if (inputData.gm == null){fromGM = 0;}
var toCur = ["USD", "EUR", "INR", "GBP", "SGD", "AUD", "CNY", "HKD", "ARS", "AED", "MXN", "NZD", "BRL", "CAD", "SEK", "COP"];
var dAmt = {"USD":10,"EUR":0,"INR":0,"GBP":0,"SGD":0,"AUD":0, "CNY":0, "HKD":0, "ARS":0, "AED":0, "MXN":0, "NZD":0, "BRL":0, "CAD":0, "SEK":0, "COP":0};
var dOOP = {"USD":0,"EUR":0,"INR":0,"GBP":0,"SGD":0,"AUD":0, "CNY":0, "HKD":0, "ARS":0, "AED":0, "MXN":0, "NZD":0, "BRL":0, "CAD":0, "SEK":0, "COP":0};
var dGM = {"USD":0,"EUR":0,"INR":0,"GBP":0,"SGD":0,"AUD":0, "CNY":0, "HKD":0, "ARS":0, "AED":0, "MXN":0, "NZD":0, "BRL":0, "CAD":0, "SEK":0, "COP":0};
output = [{amtUSD: 0,
amtEUR: 0,
amtINR: 0,
amtGBP: 0,
amtSGD: 0,
amtAUD: 0,
amtCNY: 0,
amtHKD: 0,
amtARS: 0,
amtAED: 0,
amtMXN: 0,
amtNZD: 0,
amtBRL: 0,
amtCAD: 0,
amtSEK: 0,
amtCOP: 0,
oopUSD: 0,
oopEUR: 0,
oopINR: 0,
oopGBP: 0,
oopSGD: 0,
oopAUD: 0,
oopCNY: 0,
oopHKD: 0,
oopARS: 0,
oopAED: 0,
oopMXN: 0,
oopNZD: 0,
oopBRL: 0,
oopCAD: 0,
oopSEK: 0,
oopCOP: 0,
gmUSD: 0,
gmEUR: 0,
gmINR: 0,
gmGBP: 0,
gmSGD: 0,
gmAUD: 0,
gmCNY: 0,
gmHKD: 0,
gmARS: 0,
gmAED: 0,
gmMXN: 0,
gmNZD: 0,
gmBRL: 0,
gmCAD: 0,
gmSEK: 0,
gmCOP: 0,}];
async function setOutput()
{
output["amtUSD"] = await dAmt["USD"];
output["amtEUR"] = await dAmt["EUR"];
output["amtINR"] = await dAmt["INR"];
output["amtGBP"] = await dAmt["GBP"];
output["amtSGD"] = await dAmt["SGD"];
output["amtAUD"] = await dAmt["AUD"];
output["amtCNY"] = await dAmt["CNY"];
output["amtHKD"] = await dAmt["HKD"];
output["amtARS"] = await dAmt["ARS"];
output["amtAED"] = await dAmt["AED"];
output["amtMXN"] = await dAmt["MXN"];
output["amtNZD"] = await dAmt["NZD"];
output["amtBRL"] = await dAmt["BRL"];
output["amtCAD"] = await dAmt["CAD"];
output["amtSEK"] = await dAmt["SEK"];
output["amtCOP"] = await dAmt["COP"];
output["oopUSD"] = await dOOP["USD"];
output["oopEUR"] = await dOOP["EUR"];
output["oopINR"] = await dOOP["INR"];
output["oopGBP"] = await dOOP["GBP"];
output["oopSGD"] = await dOOP["SGD"];
output["oopAUD"] = await dOOP["AUD"];
output["oopCNY"] = await dOOP["CNY"];
output["oopHKD"] = await dOOP["HKD"];
output["oopARS"] = await dOOP["ARS"];
output["oopAED"] = await dOOP["AED"];
output["oopMXN"] = await dOOP["MXN"];
output["oopNZD"] = await dOOP["NZD"];
output["oopBRL"] = await dOOP["BRL"];
output["oopCAD"] = await dOOP["CAD"];
output["oopSEK"] = await dOOP["SEK"];
output["oopCOP"] = await dOOP["COP"];
output["gmUSD"] = await dGM["USD"];
output["gmEUR"] = await dGM["EUR"];
output["gmINR"] = await dGM["INR"];
output["gmGBP"] = await dGM["GBP"];
output["gmSGD"] = await dGM["SGD"];
output["gmAUD"] = await dGM["AUD"];
output["gmCNY"] = await dGM["CNY"];
output["gmHKD"] = await dGM["HKD"];
output["gmARS"] = await dGM["ARS"];
output["gmAED"] = await dGM["AED"];
output["gmMXN"] = await dGM["MXN"];
output["gmNZD"] = await dGM["NZD"];
output["gmBRL"] = await dGM["BRL"];
output["gmCAD"] = await dGM["CAD"];
output["gmSEK"] = await dGM["SEK"];
output["gmCOP"] = await dGM["COP"];
console.log("made it");
}
async function getConversionAsync() {
var response;
var data;
//convert Amounts
response = await fetch('https://data.fixer.io/api/convert?access_key=31f3cee34159836fe0308d5d590e461d&from=' + fromCur + '&to=USD&amount=' + fromAmt);
data = await response.json();
dAmt["USD"] = await parseFloat(data.result.toString()).toFixed(2);
response = await fetch('https://data.fixer.io/api/convert?access_key=31f3cee34159836fe0308d5d590e461d&from=' + fromCur + '&to=EUR&amount=' + fromAmt);
data = await response.json();
dAmt["EUR"] = await parseFloat(data.result.toString()).toFixed(2);
response = await fetch('https://data.fixer.io/api/convert?access_key=31f3cee34159836fe0308d5d590e461d&from=' + fromCur + '&to=INR&amount=' + fromAmt);
data = await response.json();
dAmt["INR"] = await parseFloat(data.result.toString()).toFixed(2);
response = await fetch('https://data.fixer.io/api/convert?access_key=31f3cee34159836fe0308d5d590e461d&from=' + fromCur + '&to=GBP&amount=' + fromAmt);
data = await response.json();
dAmt["GBP"] = await parseFloat(data.result.toString()).toFixed(2);
response = await fetch('https://data.fixer.io/api/convert?access_key=31f3cee34159836fe0308d5d590e461d&from=' + fromCur + '&to=SGD&amount=' + fromAmt);
data = await response.json();
dAmt["SGD"] = await parseFloat(data.result.toString()).toFixed(2);
response = await fetch('https://data.fixer.io/api/convert?access_key=31f3cee34159836fe0308d5d590e461d&from=' + fromCur + '&to=AUD&amount=' + fromAmt);
data = await response.json();
dAmt["AUD"] = await parseFloat(data.result.toString()).toFixed(2);
response = await fetch('https://data.fixer.io/api/convert?access_key=31f3cee34159836fe0308d5d590e461d&from=' + fromCur + '&to=CNY&amount=' + fromAmt);
data = await response.json();
dAmt["CNY"] = await parseFloat(data.result.toString()).toFixed(2);
response = await fetch('https://data.fixer.io/api/convert?access_key=31f3cee34159836fe0308d5d590e461d&from=' + fromCur + '&to=HKD&amount=' + fromAmt);
data = await response.json();
dAmt["HKD"] = await parseFloat(data.result.toString()).toFixed(2);
response = await fetch('https://data.fixer.io/api/convert?access_key=31f3cee34159836fe0308d5d590e461d&from=' + fromCur + '&to=ARS&amount=' + fromAmt);
data = await response.json();
dAmt["ARS"] = await parseFloat(data.result.toString()).toFixed(2);
response = await fetch('https://data.fixer.io/api/convert?access_key=31f3cee34159836fe0308d5d590e461d&from=' + fromCur + '&to=AED&amount=' + fromAmt);
data = await response.json();
dAmt["AED"] = await parseFloat(data.result.toString()).toFixed(2);
response = await fetch('https://data.fixer.io/api/convert?access_key=31f3cee34159836fe0308d5d590e461d&from=' + fromCur + '&to=MXN&amount=' + fromAmt);
data = await response.json();
dAmt["MXN"] = await parseFloat(data.result.toString()).toFixed(2);
response = await fetch('https://data.fixer.io/api/convert?access_key=31f3cee34159836fe0308d5d590e461d&from=' + fromCur + '&to=NZD&amount=' + fromAmt);
data = await response.json();
dAmt["NZD"] = await parseFloat(data.result.toString()).toFixed(2);
response = await fetch('https://data.fixer.io/api/convert?access_key=31f3cee34159836fe0308d5d590e461d&from=' + fromCur + '&to=BRL&amount=' + fromAmt);
data = await response.json();
dAmt["BRL"] = await parseFloat(data.result.toString()).toFixed(2);
response = await fetch('https://data.fixer.io/api/convert?access_key=31f3cee34159836fe0308d5d590e461d&from=' + fromCur + '&to=CAD&amount=' + fromAmt);
data = await response.json();
dAmt["CAD"] = await parseFloat(data.result.toString()).toFixed(2);
response = await fetch('https://data.fixer.io/api/convert?access_key=31f3cee34159836fe0308d5d590e461d&from=' + fromCur + '&to=CNY&amount=' + fromAmt);
data = await response.json();
dAmt["CNY"] = await parseFloat(data.result.toString()).toFixed(2);
//convert OOP
response = await fetch('https://data.fixer.io/api/convert?access_key=31f3cee34159836fe0308d5d590e461d&from=' + fromCur + '&to=USD&amount=' + fromOOP);
data = await response.json();
dOOP["USD"] = await parseFloat(data.result.toString()).toFixed(2);
response = await fetch('https://data.fixer.io/api/convert?access_key=31f3cee34159836fe0308d5d590e461d&from=' + fromCur + '&to=EUR&amount=' + fromOOP);
data = await response.json();
dOOP["EUR"] = await parseFloat(data.result.toString()).toFixed(2);
response = await fetch('https://data.fixer.io/api/convert?access_key=31f3cee34159836fe0308d5d590e461d&from=' + fromCur + '&to=INR&amount=' + fromOOP);
data = await response.json();
dOOP["INR"] = await parseFloat(data.result.toString()).toFixed(2);
response = await fetch('https://data.fixer.io/api/convert?access_key=31f3cee34159836fe0308d5d590e461d&from=' + fromCur + '&to=GBP&amount=' + fromOOP);
data = await response.json();
dOOP["GBP"] = await parseFloat(data.result.toString()).toFixed(2);
response = await fetch('https://data.fixer.io/api/convert?access_key=31f3cee34159836fe0308d5d590e461d&from=' + fromCur + '&to=SGD&amount=' + fromOOP);
data = await response.json();
dOOP["SGD"] = await parseFloat(data.result.toString()).toFixed(2);
response = await fetch('https://data.fixer.io/api/convert?access_key=31f3cee34159836fe0308d5d590e461d&from=' + fromCur + '&to=AUD&amount=' + fromOOP);
data = await response.json();
dOOP["AUD"] = await parseFloat(data.result.toString()).toFixed(2);
response = await fetch('https://data.fixer.io/api/convert?access_key=31f3cee34159836fe0308d5d590e461d&from=' + fromCur + '&to=CNY&amount=' + fromOOP);
data = await response.json();
dAmt["CNY"] = await parseFloat(data.result.toString()).toFixed(2);
response = await fetch('https://data.fixer.io/api/convert?access_key=31f3cee34159836fe0308d5d590e461d&from=' + fromCur + '&to=HKD&amount=' + fromOOP);
data = await response.json();
dOOP["HKD"] = await parseFloat(data.result.toString()).toFixed(2);
response = await fetch('https://data.fixer.io/api/convert?access_key=31f3cee34159836fe0308d5d590e461d&from=' + fromCur + '&to=ARS&amount=' + fromOOP);
data = await response.json();
dOOP["ARS"] = await parseFloat(data.result.toString()).toFixed(2);
response = await fetch('https://data.fixer.io/api/convert?access_key=31f3cee34159836fe0308d5d590e461d&from=' + fromCur + '&to=AED&amount=' + fromOOP);
data = await response.json();
dOOP["AED"] = await parseFloat(data.result.toString()).toFixed(2);
response = await fetch('https://data.fixer.io/api/convert?access_key=31f3cee34159836fe0308d5d590e461d&from=' + fromCur + '&to=MXN&amount=' + fromOOP);
data = await response.json();
dOOP["MXN"] = await parseFloat(data.result.toString()).toFixed(2);
response = await fetch('https://data.fixer.io/api/convert?access_key=31f3cee34159836fe0308d5d590e461d&from=' + fromCur + '&to=NZD&amount=' + fromOOP);
data = await response.json();
dOOP["NZD"] = await parseFloat(data.result.toString()).toFixed(2);
response = await fetch('https://data.fixer.io/api/convert?access_key=31f3cee34159836fe0308d5d590e461d&from=' + fromCur + '&to=BRL&amount=' + fromOOP);
data = await response.json();
dOOP["BRL"] = await parseFloat(data.result.toString()).toFixed(2);
response = await fetch('https://data.fixer.io/api/convert?access_key=31f3cee34159836fe0308d5d590e461d&from=' + fromCur + '&to=CAD&amount=' + fromOOP);
data = await response.json();
dOOP["CAD"] = await parseFloat(data.result.toString()).toFixed(2);
response = await fetch('https://data.fixer.io/api/convert?access_key=31f3cee34159836fe0308d5d590e461d&from=' + fromCur + '&to=CNY&amount=' + fromOOP);
data = await response.json();
dOOP["CNY"] = await parseFloat(data.result.toString()).toFixed(2);
//convert GM
response = await fetch('https://data.fixer.io/api/convert?access_key=31f3cee34159836fe0308d5d590e461d&from=' + fromCur + '&to=USD&amount=' + fromGM);
data = await response.json();
dGM["USD"] = await parseFloat(data.result.toString()).toFixed(2);
response = await fetch('https://data.fixer.io/api/convert?access_key=31f3cee34159836fe0308d5d590e461d&from=' + fromCur + '&to=EUR&amount=' + fromGM);
data = await response.json();
dGM["EUR"] = await parseFloat(data.result.toString()).toFixed(2);
response = await fetch('https://data.fixer.io/api/convert?access_key=31f3cee34159836fe0308d5d590e461d&from=' + fromCur + '&to=INR&amount=' + fromGM);
data = await response.json();
dGM["INR"] = await parseFloat(data.result.toString()).toFixed(2);
response = await fetch('https://data.fixer.io/api/convert?access_key=31f3cee34159836fe0308d5d590e461d&from=' + fromCur + '&to=GBP&amount=' + fromGM);
data = await response.json();
dGM["GBP"] = await parseFloat(data.result.toString()).toFixed(2);
response = await fetch('https://data.fixer.io/api/convert?access_key=31f3cee34159836fe0308d5d590e461d&from=' + fromCur + '&to=SGD&amount=' + fromGM);
data = await response.json();
dGM["SGD"] = await parseFloat(data.result.toString()).toFixed(2);
response = await fetch('https://data.fixer.io/api/convert?access_key=31f3cee34159836fe0308d5d590e461d&from=' + fromCur + '&to=AUD&amount=' + fromGM);
data = await response.json();
dOOP["AUD"] = await parseFloat(data.result.toString()).toFixed(2);
response = await fetch('https://data.fixer.io/api/convert?access_key=31f3cee34159836fe0308d5d590e461d&from=' + fromCur + '&to=CNY&amount=' + fromGM);
data = await response.json();
dGM["CNY"] = await parseFloat(data.result.toString()).toFixed(2);
response = await fetch('https://data.fixer.io/api/convert?access_key=31f3cee34159836fe0308d5d590e461d&from=' + fromCur + '&to=HKD&amount=' + fromGM);
data = await response.json();
dGM["HKD"] = await parseFloat(data.result.toString()).toFixed(2);
response = await fetch('https://data.fixer.io/api/convert?access_key=31f3cee34159836fe0308d5d590e461d&from=' + fromCur + '&to=ARS&amount=' + fromGM);
data = await response.json();
dGM["ARS"] = await parseFloat(data.result.toString()).toFixed(2);
response = await fetch('https://data.fixer.io/api/convert?access_key=31f3cee34159836fe0308d5d590e461d&from=' + fromCur + '&to=AED&amount=' + fromGM);
data = await response.json();
dGM["AED"] = await parseFloat(data.result.toString()).toFixed(2);
response = await fetch('https://data.fixer.io/api/convert?access_key=31f3cee34159836fe0308d5d590e461d&from=' + fromCur + '&to=MXN&amount=' + fromGM);
data = await response.json();
dOOP["MXN"] = await parseFloat(data.result.toString()).toFixed(2);
response = await fetch('https://data.fixer.io/api/convert?access_key=31f3cee34159836fe0308d5d590e461d&from=' + fromCur + '&to=NZD&amount=' + fromGM);
data = await response.json();
dOOP["NZD"] = await parseFloat(data.result.toString()).toFixed(2);
response = await fetch('https://data.fixer.io/api/convert?access_key=31f3cee34159836fe0308d5d590e461d&from=' + fromCur + '&to=BRL&amount=' + fromGM);
data = await response.json();
dGM["BRL"] = await parseFloat(data.result.toString()).toFixed(2);
response = await fetch('https://data.fixer.io/api/convert?access_key=31f3cee34159836fe0308d5d590e461d&from=' + fromCur + '&to=CAD&amount=' + fromGM);
data = await response.json();
dGM["CAD"] = await parseFloat(data.result.toString()).toFixed(2);
response = await fetch('https://data.fixer.io/api/convert?access_key=31f3cee34159836fe0308d5d590e461d&from=' + fromCur + '&to=CNY&amount=' + fromGM);
data = await response.json();
dGM["CNY"] = await parseFloat(data.result.toString()).toFixed(2);
await setOutput()
}
getConversionAsync();```
答案 2 :(得分:0)
几点:
请勿使用setTimeout()
。您只能猜测超时值。 5000,或其他任何内容,可能太长或太短。允许fetch()
和response.json()
返回的承诺来确定时间很简单-无需猜测。
允许您的函数接受参数并返回结果。这样,调用者函数可以直接与返回的值/对象和外部成员一起使用,例如dAmt
,dOOP
,dGM
,fAmt
,fOOP
,{{ 1}},可以避免;大多数将成为fGM
的内部成员。
由于getConvertedAmounts()
是异步的,并且getConversionAsync
调用getConvertedAmounts()
,因此getConversionAsync()
也(根据定义)也是异步的。
无需初始化getConvertedAmounts()
。如果任何output
调用失败,则可以使用getConversionAsync()
中的错误处理来设置转换的默认值。
fetch()
的数量可以用几行代码来程序化。
您可能最终会遇到这样的情况:
output