Backend script
Script
Server side JS are executed inside an asynchronous function, and support the use of await and can return every javascript value or a Promise.
Here are some examples:
Script returning JSON
const x = {success: false, someData = [1,2,3]}
return x;Working with Promise
A backend script can return a Promise directly, Anacleto takes care of managing the promis'sthen
const queryArgs = {
db: "MASTER",
sql: "select id from table"
}
return mysql.query(queryArgs);Promise
In Anacleto all asynchronous methods work using Promises, otherwise it's a moment to end up in the callback hell 😈
Here are some examples of scripts that use promises.
In a script you can use
awaitto wait for the outcome of a promise. Don't worry,waitdoesn't block the application because your script is run inside an async function
Use
thenandcatcheto handle asynchronous functions
Compact way
And when things get serious
Organize scripts
Folder
In Anacleto you can group scripts in folders, use this feature to better organize and order your code.
May the force be with you
Import
In a backend script you can use the import keyword to import another script. Use this feature when you have code you want to reuse.
Remember that in Anacleto you can organize your scripts in folders and you can do multiple imports. Combine these 2 features to write clean, non-duplicate, and well-organized code.
Var, let or const
Anacleto supportsvar, let e const, use them!
vardeclarations are globally scoped or function scoped whileletandconstare block scoped.varvariables can be updated and re-declared within its scope;letvariables can be updated but not re-declared;constvariables can neither be updated nor re-declared.They are all hoisted to the top of their scope. But while
varvariables are initialized withundefined,letandconstvariables are not initialized.While
varandletcan be declared without being initialized,constmust be initialized during declaration.
Strings and ES6
With the advent of ES6 in 2015 working with strings has become much easier.
As you once did:
how you can do it from 2015:
TIPS: don't know how to do this strange superscript ` in a Mac? Just press ALT+ BACKSLASH
Date and Numbers
Intl, localize dates and numers
With the advent of ECMAScript all modern browsers and NodeJS support using Intl, this new object can help you a lot in formatting dates and numbers.
The Intl object is the namespace for the ECMAScript Internationalization API, which provides language sensitive string comparison, number formatting, and date and time formatting. The Intl object provides access to several constructors as well as functionality common to the internationalization constructors and other language sensitive functions. Intl doc
TIPS: don't hit your head when you define the formats, let the browser do this job!
Date vs luxon
Working with dates is never easy, anyone who has worked in JS will surely have struggled with the Date object. Unfortunately this object is very basic and just parsing or printing a date is a battle.
For this reason Intl was introduced with ECMAScript which helps us in printing.
If this object is not enough for you in Anacleto it is possible to use luxon.
Luxonis the heir ofmoment.js, the moment.js team itself recommends switching to luxon. If you don't know what moment.js is, just know that it was the most used library for working with dates in Javascript
Here are some examples of how Luxon can help you, you can find much more info directly in the official doc - luxon Docs
Last updated