{"id":670,"date":"2015-09-25T13:36:22","date_gmt":"2015-09-25T20:36:22","guid":{"rendered":"http:\/\/cknotes.com\/?p=670"},"modified":"2019-02-16T05:22:03","modified_gmt":"2019-02-16T12:22:03","slug":"asynchronous-chilkat-methods-in-node-js","status":"publish","type":"post","link":"https:\/\/cknotes.com\/asynchronous-chilkat-methods-in-node-js\/","title":{"rendered":"Asynchronous Chilkat Methods in Node.js"},"content":{"rendered":"<p>Chilkat implements a thread pool for asynchronous tasks in all programming languages <strong>except for Node.js<\/strong>. For Node.js, Chilkat tasks run directly on Node&#8217;s internal thread pool (i.e. the libuv thread pool). The Task.Run() method can have 0 or 1 arguments. If an argument is passed, it is the &#8220;task completed&#8221; callback that get called when the task has completed. Here is an example showing how to send email asynchronously using Chilkat in Node.js<\/p>\n<pre><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nvar os = require('os');\nif (os.platform() == 'win32') {  \n    if (os.arch() == 'ia32') {\n        var chilkat = require('@chilkat\/ck-node11-win-ia32');\n    } else {\n        var chilkat = require('@chilkat\/ck-node11-win64'); \n    }\n} else if (os.platform() == 'linux') {\n    if (os.arch() == 'arm') {\n        var chilkat = require('@chilkat\/ck-node11-arm');\n    } else if (os.arch() == 'x86') {\n        var chilkat = require('@chilkat\/ck-node11-linux32');\n    } else {\n        var chilkat = require('@chilkat\/ck-node11-linux64');\n    }\n} else if (os.platform() == 'darwin') {\n    var chilkat = require('@chilkat\/ck-node11-macosx');\n}\n\n\/\/ While a task is running, there are three standard callbacks that can be made (if desired).\n\/\/ They are progressInfo, percentDone, and abortCheck.   These callbacks are made directly\n\/\/ from the libuv worker thread.\n\n\/\/ progressInfo callbacks have name\/value string arguments.\n\/\/ Experiment to see what information is provided for any given task.\nfunction progressInfo(name, value) {\n    console.log(name + \": \" + value);\n}\n\n\/\/ The percentDone callback provides an integer percentage completion value, typically from 0 to 100.\n\/\/ percentDone callbacks are made only in cases where it is possible to know a percentage completed.\n\/\/ For example, establishing a socket or TLS connection will not have percentDone callbacks, because\n\/\/ it makes no sense - a client simply waits until the server accepts the connection.  It's never in a state\n\/\/ where it can be \"50% done\".\nfunction percentDone(pctDone) {\n    console.log(\"percent complete: \" + pctDone);\n    \/\/ Return true to cause the task to abort.\n    \/\/ Return false to allow the task to continue.\n    return false;\n}\n\n\/\/ The abortCheck event is called periodically to allow the application to abort an asynchronous task.\nfunction abortCheck() {\n    console.log(\"abort check\");\n    \/\/ Return true to cause the task to abort.\n    \/\/ Return false to allow the task to continue.\n    return false;\n}\n\n\/\/ The taskCompleted event is called from the main Node.js thread.\nfunction taskCompleted(task) {\n\n    \/\/ Before looking at the task result, close the connection with the SMTP server..\n    success = mailman.CloseSmtpConnection();\n    if (success != true) {\n        console.log(\"Connection to SMTP server not closed cleanly.\");\n    }\n\n    \/\/  A finished\/completed task may be one that was canceled, aborted, or truly finished.\n    \/\/  If the task was \"canceled\", it was canceled prior to actually starting.  \n\n    \/\/  If the task \"completed\", then it ran to completion, but the actual success\/failure of the method\n    \/\/  is determined by the result obtained via one of the GetResult* methods.  (A \"completed\" task will\n    \/\/  have a StatusInt equal to 7.   If the task finished, but was not completed, then it must've\n    \/\/  been aborted or canceled:\n    if (task.StatusInt != 7) {\n        console.log(\"Task did not complete.\");\n        console.log(\"task status: \" + task.Status);\n        return;\n    }\n\n    \/\/  The SendEmail method returns a boolean.  Therefore, after the task is finished,\n    \/\/  we can get the boolean result by calling GetResultBool.  This is the return value had\n    \/\/  SendEmail been called synchronously.\n    success = task.GetResultBool();\n    if (success != true) {\n        \/\/  The task's ResultErrorText contains what would have been in the LastErrorText property had\n        \/\/  the SendEmail method been called synchronously.\n        console.log(task.ResultErrorText);\n    }\n    else {\n        console.log(\"Email sent asynchronously.\");\n    }\n}\n\nfunction chilkatExample() {\n\n    \/\/  All Chilkat classes can be unlocked at once at the beginning of a program\n    \/\/  by calling UnlockBundle.  It requires a Bundle unlock code.\n    var chilkatGlob = new chilkat.Global();\n    var success = chilkatGlob.UnlockBundle(\"Anything for 30-day trial.\");\n    if (success != true) {\n        console.log(chilkatGlob.LastErrorText);\n        return;\n    }\n\n    var mailman = new chilkat.MailMan();\n\n    \/\/  Set the SMTP server and any required settings.\n    mailman.SmtpHost = \"smtp.mymailserver.com\";\n    mailman.SmtpUsername = \"myLogin\";\n    mailman.SmtpPassword = \"myPassword\";\n    mailman.StartTLS = true;\n\n    \/\/  Create a new email object\n    var email = new chilkat.Email();\n\n    email.Subject = \"This is a test\";\n    email.Body = \"This is a test\";\n    email.From = \"Chilkat Support &amp;lt;support@chilkatsoft.com&amp;gt;\";\n    success = email.AddTo(\"Chilkat Admin\",\"admin@chilkatsoft.com\");\n\n    \/\/  Call the async version of the SendEmail method to return a task object.\n    \/\/  The task object is loaded, but is in the Inert state -- meaning it is\n    \/\/  not yet scheduled to run on Node's libuv thread pool.\n    var task = mailman.SendEmailAsync(email);\n    if (task == null ) {\n        console.log(mailman.LastErrorText);\n        return;\n    }\n\n    \/\/ Hookup callbacks that are called while the task is running.\n    \/\/ Notice that these are on the mailman object, not on the Task object.\n    mailman.ProgressInfo = progressInfo;\n    mailman.PercentDone = percentDone;\n    mailman.AbortCheck = abortCheck;\n\n    \/\/  Schedule the task for running on Node's thread pool.  This changes the task's state\n    \/\/  from Inert to Live.\n    \/\/ Pass the taskCompleted function so that it runs asynchronously.\n    \/\/ If no arguments are passed to task.Run(), then it runs synchronously (following Node's conventions).\n    success = task.Run(taskCompleted);\n    if (success != true) {\n        console.log(task.LastErrorText);\n        return;\n    }\n\n    \/\/  The application continues while the email is being sent in one of Node's worker threads.\n}\n\nchilkatExample();\n\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Chilkat implements a thread pool for asynchronous tasks in all programming languages except for Node.js. For Node.js, Chilkat tasks run directly on Node&#8217;s internal thread pool (i.e. the libuv thread pool). The Task.Run() method can have 0 or 1 arguments. If an argument is passed, it is the &#8220;task completed&#8221; callback that get called when [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[425,566],"class_list":["post-670","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-async","tag-node-js"],"_links":{"self":[{"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/posts\/670","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/comments?post=670"}],"version-history":[{"count":5,"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/posts\/670\/revisions"}],"predecessor-version":[{"id":1364,"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/posts\/670\/revisions\/1364"}],"wp:attachment":[{"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/media?parent=670"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/categories?post=670"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/tags?post=670"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}