From 5c8a1ee719b6f3ff252710c84afc15a155fca4bd Mon Sep 17 00:00:00 2001 From: Pavel Murygin Date: Thu, 21 Oct 2021 23:25:43 +0300 Subject: [PATCH] fix TS code --- fluence-js/2_basics.md | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/fluence-js/2_basics.md b/fluence-js/2_basics.md index 922569c..6d69391 100644 --- a/fluence-js/2_basics.md +++ b/fluence-js/2_basics.md @@ -132,30 +132,34 @@ import { registerHelloWorld, sayHello, getRelayTime, + tellFortune, } from "./_aqua/hello-world"; // (2) async function main() { await Fluence.start({ connectTo: krasnodar[0] }); // (3) - // (3) + // (4) registerHelloWorld({ - hello: async (str) => { + hello: (str) => { console.log(str); }, getFortune: async () => { - await new Promise(resolve => { - setTimeout(resolve, 1000) - }) - return "Wealth awaits you very soon." + await new Promise((resolve) => { + setTimeout(resolve, 1000); + }); + return "Wealth awaits you very soon."; + }, }); await sayHello(); // (4) - const relayTime = await getRelayTime(); // (5) + console.log(await tellFortune()); // (6) + + const relayTime = await getRelayTime(); console.log("The relay time is: ", new Date(relayTime).toLocaleString()); - await Fluence.stop(); // (6) + await Fluence.stop(); // (7) } main(); @@ -167,11 +171,11 @@ main(); (3) A Fluence peer has to be started before running any application in Fluence Network. For the vast majority of use cases you should use `Fluence` facade to start and stop the peer. The `start` method accepts a parameters object which. The most common parameter is the address of the relay node the peer should connect to. In this example we are using the first node of the `krasnodar` network. If you do not specify the `connectTo` options will only be able to execute air on the local machine only. Please keep in mind that the init function is asynchronous. -For every exported `service XXX` definition in aqua code, the compiler provides a `registerXXX` counterpart. These functions provide a type-safe way of registering callback handlers for the services. The callbacks are executed when the appropriate service is called in aqua on the current peer. The handlers take form of the object where keys are the name of functions and the values are async functions used as the corresponding callbacks. For example in (3) we are registering handlers for `HelloWorld` service functions which outputs it's parameter to the console. Please not that the handlers can be implemented in both: synchronous and asynchronous way. The handler can be made asynchronous like any other function in javascript: either has return a Promise or has to be mark it with async keyword to take advantage of async-await pattern +For every exported `service XXX` definition in aqua code, the compiler provides a `registerXXX` counterpart. These functions provide a type-safe way of registering callback handlers for the services. The callbacks are executed when the appropriate service is called in aqua on the current peer. The handlers take form of the object where keys are the name of functions and the values are async functions used as the corresponding callbacks. For example in (4) we are registering handlers for `HelloWorld` service functions which outputs it's parameter to the console. Please not that the handlers can be implemented in both: synchronous and asynchronous way. The handler can be made asynchronous like any other function in javascript: either has return a Promise or has to be mark it with async keyword to take advantage of async-await pattern -For every exported `func XXX` definition in aqua code, the compiler provides an async function which can be directly called from typescript. In (4, 5) we are calling exported aqua function with no arguments. Note that every function is asynchronous. +For every exported `func XXX` definition in aqua code, the compiler provides an async function which can be directly called from typescript. In (5, 6) we are calling exported aqua function with no arguments. Note that every function is asynchronous. -(6) You should call `stop` when the peer is no longer needed. As a rule of thumb all the peers should be uninitialized before destroying the application. +(7) You should call `stop` when the peer is no longer needed. As a rule of thumb all the peers should be uninitialized before destroying the application. Let's try running the example: