![Yoctopuce Yocto-GPS User Manual Download Page 44](http://html1.mh-extra.com/html/yoctopuce/yocto-gps/yocto-gps_user-manual_3415557044.webp)
Instead of using parallel threads, JavaScript relies on asynchronous I/O to handle operations with a
possible long timeout: whenever a long I/O call needs to be performed, it is only triggered and but
then the code execution flow is terminated. The JavaScript engine is therefore free to handle other
pending tasks, such as UI. Whenever the pending I/O call is completed, the system invokes a
callback function with the result of the I/O call to resume execution of the original execution flow.
When used with plain callback functions, as pervasive in Node.js libraries, asynchronous I/O tend to
produce code with poor readability, as the execution flow is broken into many disconnected callback
functions. Fortunately, new methods have emerged recently to improve that situation. In particular,
the use of
Promise
objects to abstract and work with asynchronous tasks helps a lot. Any function
that makes a long I/O operation can return a
Promise
, which can be used by the caller to chain
subsequent operations in the same flow. Promises are part of EcmaScript 2015 standard.
Promise objects are good, but what makes them even better is the new
async
/
await
keywords to
handle asynchronous I/O:
• a function declared
async
will automatically encapsulate its result as a Promise
• within an
async
function, any function call prefixed with by
await
will chain the Promise
returned by the function with a promise to resume execution of the caller
• any exception during the execution of an
async
function will automatically invoke the Promise
failure continuation
Long story made short,
async
and
await
make it possible to write EcmaScript code with all benefits of
asynchronous I/O, but without breaking the code flow. It is almost like multi-threaded execution,
except that control switch between pending tasks only happens at places where the
await
keyword
appears.
We have therefore chosen to write our new EcmaScript library using Promises and
async
functions,
so that you can use the friendly
await
syntax. To keep it easy to remember,
all public methods
of
the EcmaScript library
are
async
, i.e. return a Promise object,
except
:
•
GetTickCount()
, because returning a time stamp asynchronously does not make sense...
•
FindModule()
,
FirstModule()
,
nextModule()
, ... because device detection and
enumeration always work on internal device lists handled in background, and does not require
immediate asynchronous I/O.
7.2. Using Yoctopuce library for JavaScript / EcmaScript 2017
JavaScript is one of those languages which do not generally allow you to directly access the
hardware layers of your computer. Therefore the library can only be used to access network-enabled
devices (connected through a YoctoHub), or USB devices accessible through Yoctopuce TCP/IP to
USB gateway, named
VirtualHub
.
Go to the Yoctopuce web site and download the following items:
• The Javascript / EcmaScript 2017 programming library
1
• The VirtualHub software
2
for Windows, Mac OS X or Linux, depending on your OS
Extract the library files in a folder of your choice, you will find many of examples in it. Connect your
modules and start the VirtualHub software. You do not need to install any driver.
Using the official Yoctopuce library for node.js
Start by installing the latest Node.js version (v7.6 or later) on your system. It is very easy. You can
download it from the official web site:
http://nodejs.org
. Make sure to install it fully, including npm, and
add it to the system path.
1
www.yoctopuce.com/EN/libraries.php
2
www.yoctopuce.com/EN/virtualhub.php
7. Using Yocto-GPS with JavaScript / EcmaScript
38
www.yoctopuce.com
Summary of Contents for Yocto-GPS
Page 1: ...Yocto GPS User s guide...
Page 2: ......
Page 6: ...22 Characteristics 557 Blueprint 559 Index 561...
Page 10: ...4 www yoctopuce com...
Page 14: ...8 www yoctopuce com...
Page 18: ...12 www yoctopuce com...
Page 22: ...16 www yoctopuce com...
Page 38: ...32 www yoctopuce com...
Page 42: ...36 www yoctopuce com...
Page 54: ...48 www yoctopuce com...
Page 72: ...66 www yoctopuce com...
Page 92: ...86 www yoctopuce com...
Page 122: ...116 www yoctopuce com...
Page 132: ...126 www yoctopuce com...
Page 564: ...22 Characteristics 558 www yoctopuce com...
Page 565: ......
Page 566: ......