jaggedsoft / node-binance-api

Node Binance API is an asynchronous node.js library for the Binance API designed to be easy to use.
MIT License
1.58k stars 768 forks source link

Nodejs Binance Api UnhandledPromiseRejectionWarning Error #594

Open justissue opened 3 years ago

justissue commented 3 years ago

I run into this problem when I want to place limit orders. This is my code: `const Binance = require('node-binance-api'); const binance = new Binance().options({ APIKEY: '', APISECRET: '' }); //main code

function test() { binance.marketBuy('USDTTRY', 1); }

test(); I'm facing this problem _(node:2740) UnhandledPromiseRejectionWarning: #<IncomingMessage> (node:2740) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1) (node:2740) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code._

agasnikov commented 3 years ago

All API methods are asynchronous and should be treated as such even if you don't use the returned value. So, you have 3 ways to handle that:

  1. Pass an empty callback to the method, so the method doesn't return a promise: binance.marketBuy('USDTTRY', 1, undefined, () => {});
  2. Handle the returned promise: binance.marketBuy('USDTTRY', 1).then();
  3. Use async/await: async function test() { await binance.marketBuy('USDTTRY', 1); }