How do you wait for a return from a function before moving on in node

Dylan

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)
CherryDT

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.)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do you return an object from a function?

How can I force my function to wait promises before moving on?

How to wait for openRead function to complete on one file before moving on to the next?

How do I wait for this animation to finish before moving on to the next one?

How do I wait for the iteration before moving on the next one?

How do you wait for a button to get pressed a certain amount of times before running another function?

How do you completely run an if statement before moving to the next one?

Call inner function but wait for response before moving on

How do you return object from "__main__" python function?

How do you unwrap a Result on Ok or return from the function on Err?

How do you return multiple variables from a function in python

How do you wait for the jQuery each loop to finish before continuing?

how do you wait for data to load before view in angular 2

How to make angular wait for a function to return a value before proceeding

How do I return data from a function in node?

How do I wait for the callback to finish before I return the value?

How do I wait for response from an Angular bootstrap modal window before executing next function?

How to get Express Node route to wait for function before rendering

How to wait for function to finish before continuning in Node.js

How to wait for the powershell commands to run in node-powershell before moving ahead?

How do you wait for useAuth useEffect to return the current user state?

Wait for return value from function or callback in node js

ListenableFuture - how to wait before return

How to wait for some time before moving to next line in javascript async function?

In NightwatchJS how do I make a synchronous DB call and make Nightwatch wait for the response before moving on

How do I use threads to make an action wait for another action to finish before moving on?

how to make code wait for function return in node js

How do I wait for a promise to finish before returning the variable of a function?

How do I wait for a function to be done executing before returning a value?