{"category":{"__v":2,"_id":"566e55c12c1e760d0030c56d","pages":["566e55c12c1e760d0030c56e","566e55c12c1e760d0030c56f","566e55c12c1e760d0030c570","566e55c12c1e760d0030c571","566e55c12c1e760d0030c572","566e55c12c1e760d0030c573","566e55c12c1e760d0030c574","566e55c12c1e760d0030c575","566e55c12c1e760d0030c576","566e55c12c1e760d0030c577","566e55c12c1e760d0030c578","566e55c12c1e760d0030c579","566e55c12c1e760d0030c57a","566e55c12c1e760d0030c57b","566e55c12c1e760d0030c57c","566e55c12c1e760d0030c57d","566e55c12c1e760d0030c57e","566e55c12c1e760d0030c57f","566e55c12c1e760d0030c580","566e55c12c1e760d0030c581","566e55c12c1e760d0030c582","566e55c12c1e760d0030c583","566e55c12c1e760d0030c584","566e55c12c1e760d0030c585","566e55c12c1e760d0030c586","566e55c12c1e760d0030c587","566e55c12c1e760d0030c588","566e5640c15c8f0d000ee863"],"project":"54408e54309354080070a896","version":"566e55c02c1e760d0030c56c","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2014-10-17T03:34:44.886Z","from_sync":false,"order":0,"slug":"documentation","title":"Documentation"},"project":"54408e54309354080070a896","user":"544083bee239230800071bef","version":{"__v":1,"_id":"566e55c02c1e760d0030c56c","project":"54408e54309354080070a896","createdAt":"2015-12-14T05:38:08.400Z","releaseDate":"2015-12-14T05:38:08.400Z","categories":["566e55c12c1e760d0030c56d"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"1.2.3","version":"1.2.3"},"_id":"566e55c12c1e760d0030c571","__v":0,"updates":[],"next":{"pages":[],"description":""},"createdAt":"2015-01-10T21:57:44.913Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"auth":"required","params":[],"url":""},"isReference":false,"order":3,"body":"# Asynchronous processing is new to version 1.0\nNode owes its performance to being able to [process asynchronously](http://strongloop.com/strongblog/node-js-is-faster-than-java/) and though in previous versions of node-julia, all calls were synchronous, with 1.0.0, this is no longer the case.   Advantageous yes, however, programs written against the old version using the form that is now asynchronous will break.  Keep the following in mind when considering asynchronous versus synchronous usage.\n\n## The last argument determines if the call is synchronous or asynchronous\nIf the last argument to **eval** or **exec** is a function, then the call will be done asynchronously and the last argument will be treated as a function callback\n\n### The synchronous form. \nIn this form, **eval** takes a single argument, which is a string, while **exec** expects the function name followed by any number of arguments, the last of which is not a function.  If there are multiple return values, *res* will be an array.\n\n* *res* = *julia*.**exec**(*functionName*,*arg1*,*arg2*,...,*argn*);\n* *res* = *julia*.**eval**(*string*);\n  \n### The asynchronous form.\nIn this form, **eval** takes 2 arguments, the first is a string and the second is a function, while **exec** expects the function name followed by any number of arguments, followed by a function.  In both cases, the function will be called with the error message *err* as the first argument, which will be falsey if there is no error and an error otherwise.  If there are multiple return values (e.g. a tuple), then each result component will be bound to a different variable.\n\n* *julia*.**exec**(*functionName*,*arg1*,*arg2*,...,*argn*,*function*(*err*,*res1*,*res2*,...*resn*){ ... });\n* *julia*.**eval**(*string*,*function*(*err*,*res1*,*res2*,...*resn*){ ... });\n\n## Error processing \nAs indicated above, the asynchronous form will invoke the callback with the error message *err* set to a non null value.  In comparison, the synchronous form will throw an exception if an error occurs.\n\n## Script considerations\nAll Script calls are synchronous except for **Script.exec** which has semantics as described above, so following from the example from the [Scripts documentation](doc:scripts).\n\nIf the script contains the following:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"sqrt(args[1])\",\n      \"language\": \"julia\",\n      \"name\": \"some_script.jl\"\n    }\n  ]\n}\n[/block]\nAnd invoked by the following:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"var julia = require('node-julia');\\nvar script = julia.Script('some_script.jl');               // synchronous\\n\\nconsole.log('path: ' + script.getPath());                  // synchronous\\nconsole.log('module: ' + script.getModuleName());          // synchronous\\n\\nscript.exec(1000,function(err,res) {                       // asynchronous\\n   console.log('The sqrt of 1000 is ' + res);\\n});\\n\\nconsole.log('The sqrt of 2000 is ' + script.exec(2000));   // synchronous\\n\\nsetTimeout(function(){ console.log('done.'); },1000);\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\n The following is produced (note how the output of the synchronous call is is produced first even though it is lexically second):\n\n    path: some_script.jl\n    module: njIsoMod0\n    The sqrt of 2000 is 44.721359549995796\n    The sqrt of 1000 is 31.622776601683793\n    done.","excerpt":"","slug":"synchronous-vs-asynchronous","type":"basic","title":"Synchronous vs. Asynchronous"}

Synchronous vs. Asynchronous


# Asynchronous processing is new to version 1.0 Node owes its performance to being able to [process asynchronously](http://strongloop.com/strongblog/node-js-is-faster-than-java/) and though in previous versions of node-julia, all calls were synchronous, with 1.0.0, this is no longer the case. Advantageous yes, however, programs written against the old version using the form that is now asynchronous will break. Keep the following in mind when considering asynchronous versus synchronous usage. ## The last argument determines if the call is synchronous or asynchronous If the last argument to **eval** or **exec** is a function, then the call will be done asynchronously and the last argument will be treated as a function callback ### The synchronous form. In this form, **eval** takes a single argument, which is a string, while **exec** expects the function name followed by any number of arguments, the last of which is not a function. If there are multiple return values, *res* will be an array. * *res* = *julia*.**exec**(*functionName*,*arg1*,*arg2*,...,*argn*); * *res* = *julia*.**eval**(*string*); ### The asynchronous form. In this form, **eval** takes 2 arguments, the first is a string and the second is a function, while **exec** expects the function name followed by any number of arguments, followed by a function. In both cases, the function will be called with the error message *err* as the first argument, which will be falsey if there is no error and an error otherwise. If there are multiple return values (e.g. a tuple), then each result component will be bound to a different variable. * *julia*.**exec**(*functionName*,*arg1*,*arg2*,...,*argn*,*function*(*err*,*res1*,*res2*,...*resn*){ ... }); * *julia*.**eval**(*string*,*function*(*err*,*res1*,*res2*,...*resn*){ ... }); ## Error processing As indicated above, the asynchronous form will invoke the callback with the error message *err* set to a non null value. In comparison, the synchronous form will throw an exception if an error occurs. ## Script considerations All Script calls are synchronous except for **Script.exec** which has semantics as described above, so following from the example from the [Scripts documentation](doc:scripts). If the script contains the following: [block:code] { "codes": [ { "code": "sqrt(args[1])", "language": "julia", "name": "some_script.jl" } ] } [/block] And invoked by the following: [block:code] { "codes": [ { "code": "var julia = require('node-julia');\nvar script = julia.Script('some_script.jl'); // synchronous\n\nconsole.log('path: ' + script.getPath()); // synchronous\nconsole.log('module: ' + script.getModuleName()); // synchronous\n\nscript.exec(1000,function(err,res) { // asynchronous\n console.log('The sqrt of 1000 is ' + res);\n});\n\nconsole.log('The sqrt of 2000 is ' + script.exec(2000)); // synchronous\n\nsetTimeout(function(){ console.log('done.'); },1000);", "language": "javascript" } ] } [/block] The following is produced (note how the output of the synchronous call is is produced first even though it is lexically second): path: some_script.jl module: njIsoMod0 The sqrt of 2000 is 44.721359549995796 The sqrt of 1000 is 31.622776601683793 done.