willconant / flow-js

Javascript Library for Multi-step Asynchronous Logic
MIT License
302 stars 15 forks source link

Using flow-js with promises? #15

Closed tiagosiebler closed 7 years ago

tiagosiebler commented 7 years ago

Hello,

Can flow-js also be used with promises? If so, how? I've tried leveraging this.MULTI() but the next function never fires:

const flow = require('flow');

function generateRandomNumber () {
  return new Promise(function (resolve, reject) {
    var randomNumber = Math.floor((Math.random() * 10) + 1)
    if (randomNumber <= 5) {
      resolve(randomNumber)
    } else {
      reject(randomNumber)
    }
  })
}

/*
generateRandomNumber().then(function(result) {
    console.log('Success: ' + result)
}).catch(function(error) {
    console.log('Error: ' + error)
})//*/

flow.exec(
    function(){
        generateRandomNumber().then(this.MULTI("RES1")).catch(this.MULTI("REJ1"));
        generateRandomNumber().then(this.MULTI("RES2")).catch(this.MULTI("REJ2"));

    },function(argsArray){
        debugger;
        console.log("this is the data: ",JSON.stringify(argsArray));
    }
);
console.log("EOF");

It works really well for function() callbacks, though I'd love to also leverage this for promises if possible.