barbiturat
6/8/2017 - 9:12 AM

sequence

sequence

const plusOne = x => x + 1;
const plusTwo = x => x + 2;
const plusThree = x => x + 3;

// R.converge((...args) => args, [plusOne, plusTwo, plusThree])(1); // => [2, 3, 4]
// juxt([plusOne, plusTwo, plusThree])(1) // => [2, 3, 4]
 
sequence(always, [plusOne, plusTwo, plusThree])(1) // => [2, 3, 4]

/////////////////////////////////////////////////////////////////////////////////

const getTimeFromStart = startTime => Date.now() - startTime;

const makeFuture = startTime => ms => Future((reject, resolve) => {
  console.log('start: ' + ms, 'timeFromStart: ' + getTimeFromStart(startTime));
  return setTimeout(() => {
    console.log('finish: ' + ms, 'timeFromStart: ' + getTimeFromStart(startTime)); 
    resolve(ms);
  }, ms)
});

const futureOfMs = makeFuture(Date.now());
const futuRes = R.sequence(Future.of, [futureOfMs(1000), futureOfMs(3000), futureOfMs(5000)]);

futuRes.fork(err => console.error(err), data => console.log(data));  //=>
// start: 1000
// timeFromStart: 0
// start: 5000
// timeFromStart: 0
// start: 3000
// timeFromStart: 1
// finish: 1000
// timeFromStart: 1001
// finish: 3000
// timeFromStart: 3003
// finish: 5000
// timeFromStart: 5003
// [1000,5000,3000]

// So, method starts all the futures simultaneously. It returns results according to a preassigned order.