javascript - Getting API data via asynch-await instead of callback -


i trying via kraken-node api ticker data.

i tried following ways:

import krakenclient "kraken-api"; const knex = require('knex')(require('../knexfile')) const kraken = new krakenclient();  //********************* //asynch await example* //*********************  const tickerasynch = async function() {     // ticker info     return kraken.api('ticker', { pair: 'xxbtzusd' }); }; tickerasynch().then(data => console.log(data)).catch(err => console.log(err))  //***************** //callback example* //***************** // ticker info   const tickercallback = function() {     kraken.api('ticker', { "pair": 'xxbtzusd' }, function(error, data) {         if (error) {             console.log(error);         }         else {             console.log(data.result);         }     }) };  console.log("callback: " + tickercallback()) 

the asynch await example gives me http request back:

callback: undefined request { domain: null, _events: { error: [function: bound ], complete: [function: bound ], pipe: [function] }, _eventscount: 3, _maxlisteners: undefined, method: 'post', headers: { 'user-agent': 'kraken javascript api client', host: 'api.kraken.com', 'content-type': 'application/x-www-form-urlencoded', 'content-length': 13 }, timeout: 5000, callback: [function], readable: true, writable: true, explicitmethod: true, _qs:
querystring { request: [circular], lib: { formats: [object], parse: [function], stringify: [function] }, usequerystring: undefined, parseoptions: {}, stringifyoptions: { format: 'rfc3986' } }, _auth: auth { request: [circular], hasauth: false, sentauth: false, bearertoken: null, user: null, pass: null }, _oauth: oauth { request: [circular], params: null }, _multipart: multipart { request: [circular], boundary: '839beaf0-e37d-459b-a879-0d1e2b22aab4', chunked: false, body: null }, _redirect: redirect { request: [circular], followredirect: true, followredirects: true, followallredirects: false, followoriginalhttpmethod: false, allowredirect: [function], maxredirects: 10, redirects: [], redirectsfollowed: 0, removerefererheader: false }, _tunnel: tunnel { request: [circular], proxyheaderwhitelist: [ 'accept', 'accept-charset', 'accept-encoding', 'accept-language', 'accept-ranges', 'cache-control', 'content-encoding', 'content-language', 'content-location', 'content-md5', 'content-range', 'content-type', 'connection', 'date', 'expect', 'max-forwards', 'pragma', 'referer', 'te', 'user-agent', 'via' ], proxyheaderexclusivelist: [] }, setheader: [function], hasheader: [function], getheader: [function], removeheader: [function], localaddress: undefined, pool: {}, dests: [],
__isrequestrequest: true, _callback: [function], uri: url { protocol: 'https:', slashes: true, auth: null, host: 'api.kraken.com', port: 443, hostname: 'api.kraken.com', hash: null, search: null, query: null, pathname: '/0/public/ticker', path: '/0/public/ticker', href: 'https://api.kraken.com/0/public/ticker' }, proxy: null, tunnel: true, sethost: true, originalcookieheader: undefined,
_disablecookies: true, jar: undefined, port: 443, host: 'api.kraken.com', body: 'pair=xxbtzusd', path: '/0/public/ticker', httpmodule: { server: { [function: server] super: [object] }, createserver: [function: createserver], globalagent: agent { domain: null, _events: [object], _eventscount: 1, _maxlisteners: undefined, defaultport: 443, protocol: 'https:', options: [object], requests: {}, sockets: {}, freesockets: {}, keepalivemsecs: 1000, keepalive: false, maxsockets: infinity, maxfreesockets: 256, maxcachedsessions: 100, sessioncache: [object] }, agent: { [function: agent] super: [object] }, request: [function: request], get: [function: get] }, agentclass: { [function: agent] super_: { [function: agent] super_: [object], defaultmaxsockets: infinity } }, agent: agent { domain: null, _events: { free: [function] }, _eventscount: 1, _maxlisteners: undefined, defaultport: 443, protocol: 'https:', options: { path: null }, requests: {}, sockets: {}, freesockets: {}, keepalivemsecs: 1000, keepalive: false, maxsockets: infinity, maxfreesockets: 256, maxcachedsessions: 100, _sessioncache: { map: {}, list: [] } } }

whereas via callback example prices back:

{ xxbtzusd:     { a: [ '4347.99900', '1', '1.000' ],      b: [ '4345.00000', '1', '1.000' ],      c: [ '4354.97000', '0.19747745' ],      v: [ '74.25674323', '10944.61634833' ],      p: [ '4391.05837', '4290.88239' ],      t: [ 314, 31776 ],      l: [ '4264.00000', '4082.99500' ],      h: [ '4468.00000', '4484.29000' ],      o: '4349.98700' } } 

any suggestions doing wrong within asynch-await example?

i appreciate replies!

i think kraken.api expects callback , doesn't return promise, can wrap callback promise using code below

function getkrakenpromise(){   return new promise(function(resolve, reject){             kraken.api('ticker', { "pair": 'xxbtzusd' }, function(error, data) {             if (error) {                 console.log(error);                 reject(error)             }             else {                 console.log(data.result);                 resolve(data);             }         })   }) } 

and call getkrakenpromise() instead of api


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -