Can someone explain to me why what I am doing is wrong? How do I solve this problem the right way?
I have this code to execute a trade:
async function limitOrder(keys, symbol, type, quantity, price, waitResult) {
var client = await loadKeys(keys);
return new Promise((resolve, reject) => {
client.newOrder({
symbol: symbol,
side: type, //BUY OR SELL
type: 'LIMIT',
quantity: quantity,
price: price,
timeInForce: "GTC",
newOrderRespType: 'RESULT'
}).then(async (response) => {
console.log(response);
resolve({order: response, price: response.price, quantity: response.origQty}); //return back the order ID
}
}).catch((err) => {
console.log(err); //print error if any
reject(err);
});
});
}
This is how I wrote my Promise function for the first time, and I am getting an error in the reject(err)
line. So if the order executes correctly, the resolve()
does return back the order to the function that's calling this one. But if the it reaches the catch block, the error is printed. However, the reject
line gives me this error instead:
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().
What I want to do is return the error and handle it in another function (that's calling this one).