How to pipe Promise.allSettled for lodash/fp pipe? (Or any functional like pipe lib, ex: p-pipe)

Viewed 519

Would want to:
write an entire chain of function calls as pipe (the pipe function can be from Lodash or other libs)

My working but incomplete attempt:
This works, I'm half happy with this but not 100% what I want:

import {filter, map, pipe} from 'lodash/fp';
const anAwesomeFunction = async stuff => {
const data = await fetchData(stuff);
  return {
    title: 'example',
    data
  };
};

const pipingRocks = await pipe(
   filter({status: 'fulfilled'}),
   map(
     (promiseLikeObj: PromiseFulfilledResult) => promiseLikeObj.value
   )
)(await Promise.allSettled(map(anAwesomeFunction, result)));

What I'd like to have:
As far as I read pipe from lodash doesn't get async functions: lodash fp docs

import {filter, map, pipe} from 'lodash/fp';
const anAwesomeFunction = async stuff => {
const data = await fetchData(stuff);
  return {
    title: 'example',
    data
  };
};

const pipingRocks = await pipe(
   map(anAwesomeFunction),
   Promise.allSettled, // here lies my doubt on how to bind it
   filter({status: 'fulfilled'}),
   map(
     (promiseLikeObj: PromiseFulfilledResult) => promiseLikeObj.value
   )
)(result);

I have also tried with: import pPipe from 'p-pipe';

1 Answers

So, soon after posting this question I gave one last shot and got it working with p-pipe (not with the lodash one), binding the Promise.allsettled as a Promise, just like:

import {filter, map} from 'lodash/fp';
import pPipe from 'p-pipe';

const anAwesomeFunction = async stuff => {
const data = await fetchData(stuff);
  return {
    title: 'example',
    data
  };
};

const pipingRocks = await pPipe(
   map(anAwesomeFunction),
   Promise.allSettled.bind(Promise),
   filter({status: 'fulfilled'}),
   map(
     (promiseLikeObj: PromiseFulfilledResult) => promiseLikeObj.value
   )
)(result);

I'm open to better suggestions.

Related