Skip to content

Instantly share code, notes, and snippets.

@itacirgabral
Last active July 21, 2019 14:17
Show Gist options
  • Save itacirgabral/17fc1606e47a9da218808a72d5444698 to your computer and use it in GitHub Desktop.
Save itacirgabral/17fc1606e47a9da218808a72d5444698 to your computer and use it in GitHub Desktop.
capturing side effects in a task
const P = f => {
const LazyRight = h => ({
map: f => LazyRight(() => f(h())),
then: (f, g) => g(h()),
toString: () => `Resolve(${h})`,
})
const LazyLeft = h => ({
map: f => LazyLeft(h),
then: (f, g) => f(h()),
toString: () => `Reject(${h})`,
})
const dust = Symbol()
let L = dust
let R
f(rej => L = rej, res => R = res)
return (L === dust) ? LazyRight(() => R) : LazyLeft(() => L)
};
P((rej, res) => {
if (Math.random() > 0.5) {
res(0)
} else {
rej(0)
}
}).map(n => console.log(`log ${n}`) || n + 1)
.map(n => console.log(`log ${n}`) || n + 1)
.map(n => console.log(`log ${n}`) || n + 1)
.map(n => console.log(`log ${n}`) || n + 1)
.map(n => console.log(`log ${n}`) || n + 1)
.then(
n => console.log(`Reject - n: ${n}`),
n => console.log(`Resolve - n: ${n}`)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment