[英]Try to calculate 2 values from callback (Binance Api using JavaScript)
I have 2 different code to get data: first is to get by balance account the second is to get the current value of the coin I need to calculate the number of coins that I can buy with the balance (Balance*coin value = quantity)我有 2 个不同的代码来获取数据:第一个是通过余额帐户获取第二个是获取硬币的当前价值我需要计算我可以用余额购买的硬币数量(余额 * 硬币价值 = 数量)
I have tried many options to get the value outside of the function without luck.我已经尝试了许多选项来获得 function 之外的值,但没有运气。
first code: (get balance)第一个代码:(获取余额)
binance.balance((error, balances) => {
if ( error ) return console.error(error);
console.info("BTT balance: ", balances.BTT.available);
});
Second code (get coin value)第二个代码(获取硬币价值)
binance.prices('BTTUSDT',(error, ticker)=>{
console.info("Hello:: ", ticker.BTTUSDT);
});
Dive in callback hell:潜入回调地狱:
binance.balance((error, balances) => {
if ( error ) return console.error(error);
console.info("BTT balance: ", balances.BTT.available);
binance.prices('BTTUSDT',(error, ticker)=>{
if ( error ) return console.error(error);
// Balance*coin
});
});
Or use Promises:或者使用承诺:
const getBalance = () => new Promise((resolve, reject) => {
binance.balance((error, balances) => {
if(error) {
reject(error);
return;
}
resolve(balances.BTT.available);
});
const getCoins = () => new Promise((resolve, reject) => {
binance.prices('BTTUSDT', (error, ticker) => {
if(error) {
reject(error);
return;
}
resolve(ticker.BTTUSDT);
});
(async () => {
const balance = await getBalance();
const coins = await getCoins();
console.info(balance * coins);
})();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.