I have an async function that checks for the status of an order (checkOrderStatus()). I would like to repeat this function until it returns either FILLED or CANCELED, then use this return value in another function to decide to continue or stop the code. Every order goes through different status before being FILLED or CANCELED, therefore the need to repeat the checkOrderStatus() function (it is an API call).
What I have now is this, to repeat the checkOrderStatus() function :
const watch = filter => { return new Promise(callback => { const interval = setInterval(async () => { if (!(await filter())) return; clearInterval(interval); callback(); }, 1000); }); }; const watchFill = (asset, orderId) => { return watch(async () => { const { status } = await checkOrderStatus(asset, orderId); console.log(`Order status: ${status}`); if (status === 'CANCELED') return false; return status === 'FILLED'; }); }; I then call watchFill() from another function, where I would like to check its return value (true or false) and continue the code if true or stop it if false :
const sellOrder = async (asset, orderId) => { try { const orderIsFilled = await watchFill(asset, orderId); if (orderIsFilled) { //… Continue the code (status === 'FILLED'), calling other async functions … } else { //… Stop the code return false; } } catch (err) { console.error('Err sellIfFilled() :', err); } }; However, this does not work. I can see the status being updated in the terminal via the console.log in watchFill(), but it never stops and most importantly, the value in the orderIsFilled variable in sellOrder() does not get updated, whatever the value returned by watchFill() becomes.
How can I achieve the desired behaviuor ?
Thank you very much
https://stackoverflow.com/questions/66837523/repeat-async-function-until-true March 28, 2021 at 08:55AM
没有评论:
发表评论