Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes migration docs for Task #160

Merged
merged 2 commits into from
Oct 15, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 38 additions & 6 deletions docs/_docs/v2.0.0/migrating/from-data.task.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,13 @@ function delay(ms) {
});
}

const resources = delay(1000).fork(
const delayTask = delay(1000);
const resources = delayTask.fork(
(error) => {
delay.cleanup(resources);
delayTask.cleanup(resources);
},
(value) => {
delay.cleanup(resources);
delayTask.cleanup(resources);
}
);
{% endhighlight %}
Expand All @@ -100,6 +101,37 @@ function delay(ms) {
delay(100).run();
{% endhighlight %}

To run arbitrary code in response to the result of executing the task, the `listen` method of `TaskExecution` is used in place of the functions one would pass to `Task.fork` before.

Where one used to write:

{% highlight js %}
const Task = require('data.task');

const one = new Task((reject, resolve) => resolve(1));

one.fork(
(error) => { console.log('something went wrong') },
(value) => { console.log(`The value is ${value}`) }
);
// logs "The value is 1"
{% endhighlight }

In Folktale 2 would be:

{% highlight js %}
const { task } = require('folktale/concurrency/task');

const one = task(resolver => resolver.resolve(1));

one.run().listen({
onCancelled: () => { console.log('the task was cancelled') },
onRejected: (error) => { console.log('something went wrong') },
onResolved: (value) => { console.log(`The value is ${value}`) }
});
// logs "The value is 1"
{% endhighlight %}


## Cancelling tasks

Expand Down Expand Up @@ -197,9 +229,9 @@ Folktale 2 removes both `fold` and `cata`, and provides a new method that signal
const { of } = require('folktale/data/task');

of(1).willMatchWith({
Cancelled: () => 'cancelled',
Resolved: (value) => value + 1,
Rejected: (error) => error - 1
Cancelled: () => of('cancelled'),
Resolved: (value) => of(value + 1),
Rejected: (error) => of(error - 1)
});
// Task.of(2)
{% endhighlight %}
Expand Down