{"_id":"561a897730e7490d0022d663","user":"5617a0a4a410c90d00c612f4","category":{"_id":"5619a39a13874c0d007038ec","project":"5617a1f5a410c90d00c612f5","__v":9,"pages":["561a88d530e7490d0022d661","561a88de13874c0d0070394c","561a897730e7490d0022d663","5651055ea8567917000f71e9","56510675481fd91700dffe25","565106d8c042820d00de3a54","5652003550c76e0d00615a49","566bfb5a4db9c60d00c9a833","566bfbba7831040d003eb385"],"version":"5619a39913874c0d007038e9","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2015-10-09T13:15:56.785Z","from_sync":false,"order":5,"slug":"modules","title":"Natron Modules"},"version":{"_id":"5619a39913874c0d007038e9","project":"5617a1f5a410c90d00c612f5","__v":5,"createdAt":"2015-10-10T23:47:37.777Z","releaseDate":"2015-10-10T23:47:37.777Z","categories":["5619a39a13874c0d007038ea","5619a39a13874c0d007038eb","5619a39a13874c0d007038ec","5619a39a13874c0d007038ed","5651062a3b4d113500ed22d1","5651c89a852cfb3500a69830","565505ad055aa40d006f0ca7","56c219d8d3199a0d00694be6"],"is_deprecated":false,"is_hidden":false,"is_beta":true,"is_stable":true,"codename":"Orange Arrow","version_clean":"0.2.0","version":"0.2"},"__v":35,"project":"5617a1f5a410c90d00c612f5","updates":[],"next":{"pages":[],"description":""},"createdAt":"2015-10-11T16:08:23.650Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":1,"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]","excerpt":"Tasks with Promises","slug":"module-natron-core","type":"basic","title":"natron-core"}

natron-core

Tasks with Promises

[![Version][npm-img]][npm-url] [![Downloads][dlm-img]][npm-url] [![Build Status][travis-img]][travis-url] [npm-img]: https://img.shields.io/npm/v/natron-core.svg [npm-url]: https://npmjs.org/package/natron-core [dlm-img]: https://img.shields.io/npm/dm/natron-core.svg [travis-img]: https://travis-ci.org/natronjs/natron-core.svg [travis-url]: https://travis-ci.org/natronjs/natron-core ## Creating Tasks Tasks 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`. [block:code] { "codes": [ { "code": "import {task} from \"natron-core\";\n\nlet myTask = task(() => {\n return 42;\n}, {name: \"answer\"});", "language": "javascript" } ] } [/block] Examples: * `task(function () {})` * `task(() => {}, {name: "func"})` * `task([a, b, c])` ... a task sequence, where `a`, `b` and `c` are of type `Thing` * `task([[x, y, z]])` ... an array, as the only element, inside an array will result in a task set * `task([a, [[x, y]], b])` ... `a -> (x || y) -> b` * `task("func")` ... a lazy task, see [Task Resolver](doc:task-resolver) for more information ## Running Tasks A `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. [block:code] { "codes": [ { "code": "let greetTask = task((name) => {\n return `Hello ${name}`;\n});\n\ngreetTask.run(\"World\").then((val) => {\n console.log(val);\n});", "language": "javascript" } ] } [/block] Another 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. [block:code] { "codes": [ { "code": "greetTask.runWithContext({args: [\"World\"]});", "language": "javascript" } ] } [/block] ## Sequence `task([a, b, c, ...])`: `a -> b -> c` Tasks will be run in sequence [block:code] { "codes": [ { "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);", "language": "javascript" } ] } [/block] ### Options #### `pipe` [block:code] { "codes": [ { "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);", "language": "javascript" } ] } [/block] ## Set `task([[a, b, c, ...]])`: `a || b || c` Tasks will be run in parallel [block:code] { "codes": [ { "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);", "language": "javascript" } ] } [/block] ## Compose Complex Tasks [block:code] { "codes": [ { "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);", "language": "javascript" } ] } [/block] ## Lazy Tasks [block:code] { "codes": [ { "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});", "language": "javascript" } ] } [/block] ## Event Aggregator [block:code] { "codes": [ { "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);", "language": "javascript" } ] } [/block]