3
如果我有一個Task
對於成功值爲Either err b
,我如何組合/合併/轉換它們,以便成功值可以直接在.fork()
中使用,而不是包含在Either
中? 可以將`Either`類型轉換爲`Task`類型嗎?
const Task = require('data.task'); // folktale
const Either = require('data.either');
// eitherYayNay :: Bool → Either String String
const eitherYayNay = bool =>
bool ? Either.Right('yay') : Either.Left('nay');
// theTask :: Bool → Task Either a b
const theTask = yn =>
new Task((reject, resolve) => {
resolve(eitherYayNay(yn));
// reject();
});
// niceTask :: Bool → Task a b
// ???
// the desired result...
niceTask(something).fork(
err => {
// err could be the left value of the Task, or of the Either
},
val => {
console.log(val); // a string, not an Either
}
);