...
Code Block |
---|
|
let someString = 'some-string'
let someNumber = 69
let someBool = true
const array = ['string', 69, true]
const object = {
key: 'value',
'key-word': 'value',
} |
String formatting
use backticks
and ${}
Code Block |
---|
|
const string = 'pricefx'
const formatted = `Hi ${string}`
console.log(formatted) // Hi pricefx |
Flow control
Code Block |
---|
|
//if else
let i = 0;
if (i < 0) {
console.log("its negative");
} else if (i > 0) {
console.log("its more than 0");
} else {
console.log("its 0");
}
// while loop
let i = 0;
while (i < 5) {
console.log(i);
i++;
}
// for loop
for (let i = 0; i < 5; i++) {
console.log(i);
}
// for of loop
const array = [0, 1, 2, 3, 4];
for (const item of array) {
console.log(item);
} |
...
Code Block |
---|
|
// Initialize an object with four properties
const object = {one: 1, two: 2, three: 3, four: 4};
// Use object destructuring to assign the values of the 'one' and 'two' properties to variables,
// and the rest of the properties to a new object called 'rest'
const {one, two, ...rest} = object;
// Log the values of the variables to the console
console.log(one, two, rest); // Outputs: 1, 2, {three: 3, four: 4} |
String formatting
...
Promises (await, then)
In JavaScript, await
and then
are used to handle asynchronous operations, for example when fetching data from CRM.
Await
await
is a keyword that can be used inside an async
function to pause the execution of the function until a Promise is resolved or rejected. This allows us to write asynchronous code in a more synchronous-looking way, making it easier to understand and maintain.
Here's an example of using await
to get payload from CRM:
Code Block |
---|
|
const fetchData = async (crmManager) {
const response = await crmManager.getPayload();
console.log(fetchData);
}
fetchData(); // object with data from CRM |
Without await
, you would get unfulfilled
promise which doesn't have anything to return or show:
Code Block |
---|
|
const stringfetchData = 'pricefx' (crmManager) {
const formattedresponse = `Hi ${string}` crmManager.getPayload();
console.log(fetchData);
}
fetchData(); // Promise<Pending> |
Then
then
is a method used to handle the resolution or rejection of a Promise by providing callback functions to be executed when the Promise is resolved or rejected, allowing for the chaining of asynchronous operations.
This is not as complicated as it may seem.
Here's an example of using then
to handle getPayload:
Code Block |
---|
crmManager.getPayload()
.then((result) => {
console.log(result);
})
.catch((error) => {
console.log(formattederror);
// Hi pricefx}); |