{"metadata":{"image":[],"title":"","description":""},"api":{"url":"","auth":"required","params":[],"results":{"codes":[]},"settings":""},"next":{"description":"","pages":[]},"title":"natron-core","type":"basic","slug":"module-natron-core","excerpt":"Tasks with Promises","body":"[![Version][npm-img]][npm-url] [![Downloads][dlm-img]][npm-url] [![Build Status][travis-img]][travis-url]\n\n[npm-img]: https://img.shields.io/npm/v/natron-core.svg\n[npm-url]: https://npmjs.org/package/natron-core\n[dlm-img]: https://img.shields.io/npm/dm/natron-core.svg\n[travis-img]: https://travis-ci.org/natronjs/natron-core.svg\n[travis-url]: https://travis-ci.org/natronjs/natron-core\n\n## Creating Tasks\n\nTasks can be created using the `task(thing: Thing, meta: Object)` function. A `Thing` is a `Task` or anything that can be converted to a task, like `Function`, `Array<Thing>`, `Set<Thing>` or `string`. Additionally this function can take `meta` information that will be added to the task, e.g. a `name`.\n[block:code]\n{\n \"codes\": [\n {\n \"code\": \"import {task} from \\\"natron-core\\\";\\n\\nlet myTask = task(() => {\\n return 42;\\n}, {name: \\\"answer\\\"});\",\n \"language\": \"javascript\"\n }\n ]\n}\n[/block]\nExamples:\n* `task(function () {})`\n* `task(() => {}, {name: \"func\"})`\n* `task([a, b, c])` ... a task sequence, where `a`, `b` and `c` are of type `Thing`\n* `task([[x, y, z]])` ... an array, as the only element, inside an array will result in a task set\n* `task([a, [[x, y]], b])` ... `a -> (x || y) -> b`\n* `task(\"func\")` ... a lazy task, see [Task Resolver](doc:task-resolver) for more information\n\n## Running Tasks\n\nA `Task` can be executed by calling the `run(...args: any)` method on the task object which always returns a `Promise`. The parameters will be passed to the encapsulated function within the task. \n[block:code]\n{\n \"codes\": [\n {\n \"code\": \"let greetTask = task((name) => {\\n return `Hello ${name}`;\\n});\\n\\ngreetTask.run(\\\"World\\\").then((val) => {\\n console.log(val);\\n});\",\n \"language\": \"javascript\"\n }\n ]\n}\n[/block]\nAnother way to run a task is to use the `runWithContext(c: TaskContext)` method which expects either a valid `TaskContext` or an `Object`. This method allows a more fine-grained execution, see [Task Context](doc:task-context) for more information.\n[block:code]\n{\n \"codes\": [\n {\n \"code\": \"greetTask.runWithContext({args: [\\\"World\\\"]});\",\n \"language\": \"javascript\"\n }\n ]\n}\n[/block]\n## Sequence\n\n`task([a, b, c, ...])`: `a -> b -> c`\n\nTasks will be run in sequence\n[block:code]\n{\n \"codes\": [\n {\n \"code\": \"import {task} from \\\"natron-core\\\";\\n\\nfunction fn1(x) { return x * 1; }\\nfunction fn2(x) { return x * 2; }\\nfunction fn3(x) { return x * 3; }\\n\\n// fn1(2) -> fn2(2) -> fn3(2)\\n(task([fn1, fn2, fn3]).run(2)\\n .then((res) => {\\n // res = [2, 4, 6]\\n })\\n .catch((err) => {\\n // handle error\\n })\\n);\",\n \"language\": \"javascript\"\n }\n ]\n}\n[/block]\n### Options\n\n#### `pipe`\n[block:code]\n{\n \"codes\": [\n {\n \"code\": \"import {task} from \\\"natron-core\\\";\\n\\nfunction fn(v) { return `<${v}>`; }\\n\\n// => fn(\\\".\\\") => fn(\\\"<.>\\\") => fn(\\\"<<.>>\\\")\\n(task([fn, fn, fn], {\\n options: {pipe: true},\\n}).run(\\\".\\\")\\n .then((res) => {\\n // res = \\\"<<<.>>>\\\"\\n })\\n .catch((err) => {\\n // handle error\\n })\\n);\",\n \"language\": \"javascript\"\n }\n ]\n}\n[/block]\n## Set\n\n`task([[a, b, c, ...]])`: `a || b || c`\n\nTasks will be run in parallel\n[block:code]\n{\n \"codes\": [\n {\n \"code\": \"import {task} from \\\"natron-core\\\";\\n\\nfunction fn1(x) { return x * 1; }\\nfunction fn2(x) { return x * 2; }\\nfunction fn3(x) { return x * 3; }\\n\\n// fn1(2) || fn2(2) || fn3(2)\\n(task([[fn1, fn2, fn3]]).run(2)\\n .then((res) => {\\n // res = [2, 4, 6]\\n })\\n .catch((err) => {\\n // handle error\\n })\\n);\",\n \"language\": \"javascript\"\n }\n ]\n}\n[/block]\n## Compose Complex Tasks\n[block:code]\n{\n \"codes\": [\n {\n \"code\": \"import {task} from \\\"natron-core\\\";\\n\\nfunction fn1(x) { return x * 1; }\\nfunction fn2(x) { return x * 2; }\\nfunction fn3(x) { return x * 3; }\\n\\n// => fn1(2) -> (fn2(2) || fn3(2))\\n(task([fn1, [[fn2, fn3]]]).run(2)\\n .then((res) => {\\n // res = [2, [4, 6]]\\n })\\n .catch((err) => {\\n // handle error\\n })\\n);\",\n \"language\": \"javascript\"\n }\n ]\n}\n[/block]\n## Lazy Tasks\n[block:code]\n{\n \"codes\": [\n {\n \"code\": \"import {task} from \\\"natron-core\\\";\\n\\nfunction answer() {\\n return 4;\\n}\\n\\nlet store = {\\n answer: () => {\\n return 42;\\n },\\n};\\n\\nlet myTask = task([answer, \\\"answer\\\"], {\\n resolver: (name) => {\\n return store[name];\\n },\\n});\\n\\nmyTask.run().then((val) => {\\n // val = [4, 42]\\n});\",\n \"language\": \"javascript\"\n }\n ]\n}\n[/block]\n## Event Aggregator\n[block:code]\n{\n \"codes\": [\n {\n \"code\": \"import {EventEmitter} from \\\"events\\\";\\nimport {task} from \\\"natron-core\\\";\\n\\nlet ee = new EventEmitter();\\n\\nee.on(\\\"start\\\", (e) => {\\n // e = {task, context}\\n});\\n\\nee.on(\\\"finish\\\", (e) => {\\n // e = {task, context, value}\\n});\\n\\nee.on(\\\"error\\\", (e) => {\\n // e = {task, context, error}\\n});\\n\\n(task(/* ... */).runWithContext({\\n args: [/* ... */],\\n eventAggregator: ee,\\n})\\n .then((res) => {\\n // ...\\n })\\n);\",\n \"language\": \"javascript\"\n }\n ]\n}\n[/block]","updates":[],"order":1,"isReference":false,"hidden":false,"sync_unique":"","link_url":"","link_external":false,"_id":"561a897730e7490d0022d663","createdAt":"2015-10-11T16:08:23.650Z","user":"5617a0a4a410c90d00c612f4","category":{"sync":{"isSync":false,"url":""},"pages":["561a88d530e7490d0022d661","561a88de13874c0d0070394c","561a897730e7490d0022d663","5651055ea8567917000f71e9","56510675481fd91700dffe25","565106d8c042820d00de3a54","5652003550c76e0d00615a49","566bfb5a4db9c60d00c9a833","566bfbba7831040d003eb385"],"title":"Natron Modules","slug":"modules","order":5,"from_sync":false,"reference":false,"_id":"5619a39a13874c0d007038ec","project":"5617a1f5a410c90d00c612f5","__v":9,"createdAt":"2015-10-09T13:15:56.785Z","version":"5619a39913874c0d007038e9"},"githubsync":"","version":{"version":"0.2","version_clean":"0.2.0","codename":"Orange Arrow","is_stable":true,"is_beta":true,"is_hidden":false,"is_deprecated":false,"categories":["5619a39a13874c0d007038ea","5619a39a13874c0d007038eb","5619a39a13874c0d007038ec","5619a39a13874c0d007038ed","5651062a3b4d113500ed22d1","5651c89a852cfb3500a69830","565505ad055aa40d006f0ca7","56c219d8d3199a0d00694be6"],"_id":"5619a39913874c0d007038e9","createdAt":"2015-10-10T23:47:37.777Z","project":"5617a1f5a410c90d00c612f5","__v":5,"releaseDate":"2015-10-10T23:47:37.777Z"},"__v":35,"project":"5617a1f5a410c90d00c612f5"}
natron-core
Tasks with Promises