function send_cash_toClient_QIWI(){ this.phone = this.tradeoffer.phone_number; this.amount = this.tradeoffer.amount; let msgProccess = (msg.send + this.phone + ' Количество: ' + this.amount + ' руб.'); return this.message(msgProccess).then((text) => { return qiwi.send(One, this.amount, this.phone).then((status) => { if(status){ console.log(this.id + ': ' + 'qiwi money was send'); this.payed = true; this.save({'pay': true}); return this.message(msg.success).then((status) => { return client.markpaid(this.id).then((status)=>{ return ('Marked as Payed'); }) }) } else { return ('error send cash'); } }) }) } 

each function inside the send_cash_toClient function contains a promise (return new Promise) and returns resolve, so after each function .then ((status), and I call this function (send_cash_toClie ....) via await / async if I did not overdo it with return I originally wrote this

  function send_cash_toClient_QIWI(){ this.phone = this.tradeoffer.phone_number; this.amount = this.tradeoffer.amount; let msgProccess = (msg.send + this.phone + ' Количество: ' + this.amount + ' руб.'); return new Promise((resolve, reject) => { this.message(msgProccess).then((text) => { qiwi.send(One, this.amount, this.phone).then((status) => { if(status){ console.log(this.id + ': ' + 'qiwi money was send'); this.payed = true; this.save({'pay': true}); this.message(msg.success).then((status) => { client.markpaid(this.id).then((status)=>{ resolve('Marked as Payed') }) }) } else { resolve('error send cash') } }) }) }) } 

How correct? and so that there are no piles

    2 answers 2

    In general, promises were made in order to avoid callback hell, so it is worth uniting them in a common chain:

     function send_cash_toClient_QIWI() { this.phone = this.tradeoffer.phone_number; this.amount = this.tradeoffer.amount; let msgProccess = (msg.send + this.phone + ' Количество: ' + this.amount + ' руб.'); return this.message(msgProccess) .then(text => qiwi.send(One, this.amount, this.phone)) .then(status => { if (status) { console.log(this.id + ': ' + 'qiwi money was send'); this.payed = true; this.save({ 'pay': true }); return this.message(msg.success) .then((status) => client.markpaid(this.id)) .then((status) => ('Marked as Payed')) } else { return ('error send cash'); } }) } 

    Returning promis to then, its result falls into the next then in one chain, and not into the depths.

    Well, in the switch function return short blocks are written much cleaner - (a) => { return a; } (a) => { return a; } can be written as a => a

      Can be rewritten using await/async , will be a bit more readable.

       async function send_cash_toClient_QIWI() { this.phone = this.tradeoffer.phone_number; this.amount = this.tradeoffer.amount; let msgProccess = (msg.send + this.phone + ' Количество: ' + this.amount + ' руб.'); try { const text = await this.message(msgProccess); const status = await qiwi.send(One, this.amount, this.phone); if (status) { console.log(this.id + ': ' + 'qiwi money was send'); this.payed = true; this.save({'pay': true }); const statusSuccess = await this.message(msg.success); const markpaidResult = await client.markpaid(this.id); return 'Marked as Payed'; } else { return 'error send cash'; } } catch (e) { return e; } }