1. Asynchronous JS code style
There are two main types of asynchronous code style you'll come across in JavaScript:
Callback based Example :
setTimeout(),setInterval()Promise based Example :
fetch()
2. Creating your own Promises
Promises are the new style of async code that you'll see used in modern JavaScript.
Syntax :
In the above syntax:
- The Promise constructor takes a function (an executor) that will be executed immediately and passes in two functions: resolve, which must be called when the Promise is resolved (passing a result), and reject when it is rejected (passing an error).
- The executor is to be executed by the constructor, during the process of constructing the new Promise object.
- resolveFunction is called on promise fulfilled.
- rejectFunction is called on promise rejection.
Example :
2.1 Accessing Arguments from Resolve
When
Example :
2.2 Accessing Arguments from Reject
When
Example :
3. Async/Await
- The Async/Await is a modern way to consume promises.
- The Await ensures processing completes before the next statement executes.
Syntax :
- Use async keyword before the function only if it is performing async operations.
- Should use await inside an async function only.
3.1 Fetch with Async and Await
Example :
3.2 Error Handling with Async and Await
Example :
3.3 Async Function always returns Promise
Example :
4. String Manipulations
There are methods and properties available to all strings in JavaScript.
String Methods | Functionality |
---|---|
toUpperCase(), toLowerCase() | Converts from one case to another |
includes(), startsWith(), endsWith() | Checks a part of the string |
split() | Splits a string |
toString() | Converts number to a string |
trim(), replace() | Updates a string |
concat(), slice(), substring() | Combines & slices strings |
indexOf() | Finds an index |
4.1 trim()
The
Syntax :
4.2 slice()
The
Syntax :
4.3 toUpperCase()
The
Syntax :
4.4 toLowerCase()
The
Syntax :
4.5 split()
The
Syntax :
4.6 replace()
The
Syntax :
4.7 includes()
The
includes()method determines whether a string contains the characters of a specified string.It returns
trueif the string contains the value, otherwise it returnsfalse.
Syntax :
4.8 concat()
The
Syntax :
4.9 indexOf()
The
Syntax :
4.10 startsWith()
The
Syntax :
4.11 endsWith()
The
Syntax :
4.12 toString()
The
Syntax :
4.13 substring()
The substring() method returns the part of the string between the start and end indexes, or to the end of the string.
Syntax :
4.14 Length
The
Syntax :
Try out different string manipulations in the JavaScript Code Playground.
Practice Async Await with Promises
Search Item in a Mart Given an array mart of objects in the prefilled code and categoryOfItem , item as inputs, create a JS promise, resolve with "Item Found" text, if the categoryOfItem matches with the category and the corresponding items list includes the item reject with "Category Not Found" text, if the categoryOfItem does not match with any category in the mart reject with "Item Not Found" text, if the items list does not include item Use async/await and try/catch blocks with the help of find() and includes()