0

I'm new to Node so I'm struggling with this. The script runs as if its "skipping" the bitx.getTicker function inside the while loop. I did some research on asynchronous functions in Node and I just cant seem to get it working on my code. I'm trying to write a crypto-bot with bitx. I tried the .then() method as well and it just keeps on "skipping" the bitx.getTicker function. If I remove the async(from line 1), let tick = await``(from line 5) and the while``` loop it works as intended but just once. I need it to run constantly to check new asking price so once wont work. Thanks in advance.

async function get_ticker(){ let BitX = require('../lib/BitX') while(true){ let bitx = new BitX(key, secret) let tick = await bitx.getTicker(function (err, ticker) { // sends ticker object to another function to get values get_info(ticker) }) console.log(i) i = i + 1 } } 

UPDATE I used a promise and I see it pending but I cant access the ticker variable inside the bitx.getTicker function. How can I access it?

function get_ticker() { let BitX = require('../lib/BitX') const promise = new Promise((resolve, reject) => { let bitx = new BitX(key, secret) bitx.getTicker(function (err, ticker) { resolve(ticker) }) }) return promise.resolve } var result = get_ticker() console.log(result) 
2
  • It keeps skipping because you have passed a callback to getTicker method. You can either use callback-based API or use promises if BitX provides a method that returns a promise. With your current code, there's no point in awaiting bite.getTicker. If bite.getTicker doesn't return a promise, you can promising it using node's built-in util module. Commented Aug 20, 2022 at 12:27
  • Thanks for your answer @Yousaf. I updated my question and it looks like it works fine now but how would you access the ticker variable? I tried it in the above and I see the promise pending but I cant access the variable? Commented Aug 20, 2022 at 12:48

1 Answer 1

1

It seems like your BitX library doesn't have a promise interface but is callback-based, so you need to promisify it:

const { promisify } = require('util') async function get_ticker(){ let BitX = require('../lib/BitX') while(true){ let bitx = new BitX(key, secret) let tick = await promisify(bitx.getTicker).call(bitx) get_info(tick) console.log(i) i = i + 1 } } 

It would be more performant to create the client only once though:

const { promisify } = require('util') const BitX = require('../lib/BitX') const bitx = new BitX(key, secret) const getBitxTicker = promisify(bitx.getTicker).bind(bitx) async function get_ticker(){ while(true){ const tick = await getBitxTicker() get_info(tick) console.log(i) i = i + 1 } } 

(Note that I used bind instead of call now for binding the bitx instance, because we don't immediately call it now.)

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the reply @CherryDT. It works perfect if I pass tick`` to the get_info``` function. I just want to ask, if I only created the client once, wont I receive the same values the whole time? I want to receive live values, that's why I thought to create it repeatedly. Please let me know. Thanks :)
You still ask the (same) client every time to check the values by calling its getTicker function so you won't receive the same.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.