From f7d7226987101cbbae70136d4871609547f92df5 Mon Sep 17 00:00:00 2001 From: Quildreen Motta Date: Sun, 15 Oct 2017 15:54:47 -0200 Subject: [PATCH 1/2] Fixes task.willMatchWith example in migration docs (#159) --- docs/_docs/v2.0.0/migrating/from-data.task.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/_docs/v2.0.0/migrating/from-data.task.md b/docs/_docs/v2.0.0/migrating/from-data.task.md index 5ec6fa9..a38a16f 100644 --- a/docs/_docs/v2.0.0/migrating/from-data.task.md +++ b/docs/_docs/v2.0.0/migrating/from-data.task.md @@ -197,9 +197,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 %} From 9ab434da814424c043762cc3b211fe45f752620b Mon Sep 17 00:00:00 2001 From: Quildreen Motta Date: Sun, 15 Oct 2017 16:08:00 -0200 Subject: [PATCH 2/2] Adds docs on migrating from .fork, fixes data.task's example --- docs/_docs/v2.0.0/migrating/from-data.task.md | 38 +++++++++++++++++-- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/docs/_docs/v2.0.0/migrating/from-data.task.md b/docs/_docs/v2.0.0/migrating/from-data.task.md index a38a16f..c92b01b 100644 --- a/docs/_docs/v2.0.0/migrating/from-data.task.md +++ b/docs/_docs/v2.0.0/migrating/from-data.task.md @@ -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 %} @@ -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