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.

You are not forced to return a string, the server will arrange to return the data with the correct content type.

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 await to wait for the outcome of a promise. Don't worry, wait doesn't block the application because your script is run inside an async function

  • Use then and catche to handle asynchronous functions

  • Compact way

  • And when things get serious

Remember, Promise are your friends!

Organize scripts

Folder

In Anacleto you can group scripts in folders, use this feature to better organize and order your code.

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.

Don't worry if some scripts import the same scripts or if some scripts import each other. Anacleto will manage the dependencies for you avoiding recursively importing the same script several times.

Var, let or const

Anacleto supportsvar, let e const, use them!

  • var declarations are globally scoped or function scoped while let and const are block scoped.

  • var variables can be updated and re-declared within its scope; let variables can be updated but not re-declared; const variables can neither be updated nor re-declared.

  • They are all hoisted to the top of their scope. But while var variables are initialized with undefined, let and const variables are not initialized.

  • While var and let can be declared without being initialized, const must 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.

Luxon is the heir of moment.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