Ramda promise pipeline

Ramda 原本是提供 pipeP 函数的,但是新版本这个函数已经标记为废弃的,不建议使用,那么当需要 pipe Promise 的时候,使用哪个函数?答案是还是使用 pipe 函数,通过 andThen,或者 otherwise 将其串联起来。 示例:

function request(opts) {
    return new Promise((resolve, reject) => {
        const req = https.request(opts, res => {
            resolve(res);
        });
        req.on('error', err => reject(err));
        req.end();
    });
}

function json(res) {
    return new Promise((resolve, reject) => {
        let data = '';
        res.on('data', d => {
            data += d;
        });
        res.on('end', () => {
            try {
                resolve(JSON.parse(data));
            } catch (err) {
                reject(err);
            }
        });
        res.on('error', err => reject(err));
    });
}

const client = Ramda.pipe(
    request,
    Ramda.andThen(x => {
        return x.statusCode === 200 ? x : null;
    }),
    Ramda.andThen(json),
    Ramda.andThen(Ramda.pick(['data'])),
    Ramda.otherwise(Ramda.always(null))
);

const main = async () => {
    const result = await client(opts);
    console.log(result);
};