通过asynchronous等待而不是callback来获取API数据

我正在尝试通过kraken-node API获取数据。

我尝试了以下方法:

import KrakenClient from "kraken-api"; const knex = require('knex')(require('../knexfile')) const kraken = new KrakenClient(); //********************* //ASYNCH AWAIT EXAMPLE* //********************* const tickerAsynch = async function() { // Get Ticker Info return kraken.api('Ticker', { pair: 'XXBTZUSD' }); }; tickerAsynch().then(data => console.log(data)).catch(err => console.log(err)) //***************** //CALLBACK EXAMPLE* //***************** // Get 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()) 

ASYNCH AWAIT EXAMPLE只是给了我一个http请求:

callback:undefined Request {domain:null,_events:{error:[Function:bound],complete:[Function:bound],pipe:[Function]},_eventsCount:3,_maxListeners:undefined,方法:'POST' :{'User-Agent':'Kraken Javascript API Client',主机:'api.kraken.com','content-type':'application / x-www-form-urlencoded','content-length':13 },timeout:5000,callback:[Function],可读性:true,可写: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 {请求:[Circular],边界:'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 ','接受编码','接受语言','接受范围','caching控制','内容编码','内容语言','内容位置','content-md5', '内容范围',' proxyHeaderExclusiveList:[]内容types','connection','date','expect','max-forwards','pragma','referer','te','user-agent' setHeader:[Function],hasHeader:[Function],getHeader:[Function],removeHeader:[Function],localAddress:undefined,pool:{},dests:[],
__isRequestRequest:true,_callback:[Function],uri:Url {protocol:'https:',斜杠:true,auth:null,主机:'api.kraken.com',端口:443,主机名:'api.kraken。search:null,查询:null,path名:'/ 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:请求:{},套接字:{},freeSockets:{},keepAliveMsecs:1000,keepAlive:false,maxSockets:Infinity,maxFreeSockets:256,maxCachedSessions:100, sessionCache:[Object] {[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},请求:{},套接字:{},freeSockets:{},keepAliveMsecs:1000, keepAlive:false,maxSockets:Infinity,maxFreeSockets:256,maxCachedSessions:100,_sessionCache:{map:{},list:[]}}}

而我通过callback的例子得到的价格回来:

 { 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' } } 

任何意见,我在做什么我的asynch-await例子吗?

我很感激你的回复!

我认为kraken.api需要一个callback,并没有返回一个承诺,你可以总是使用下面的代码包装一个承诺

 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); } }) }) } 

然后调用getKrakenPromise()而不是api