Promises
Promises in JavaScript manage asynchronous operations, representing eventual results or errors. They simplify handling async code with methods like .then(), .catch(), and allow chaining.
Syntax:
const promise = new Promise((resolve,reject) => {....});
Example: In this example, The prom function returns a Promise based on the complete parameter, resolving with “success” if complete, and rejecting with “failed” otherwise.
Javascript
function prom(complete) { return prom = new Promise( function (resolve, reject) { console.log( "data iss fetching. please wait" ) if (complete) { resolve( "i am successfull" ) } else { reject( "i am failed" ) } }) } prom( true ).then((result) => console.log(result) ). catch ((error) => console.log(error)) |
Output
data iss fetching. please wait i am successfull
JS 2015 or ECMAScript 6 (ES6)
JS 2015 (ES6) also known as ECMAScript 6 (ES6), ECMAScript 6 (ES6) is a significant update to JavaScript, introducing arrow functions, classes, template literals, let and const for variable declaration, enhanced object literals, destructuring, and more modern features for better code organization and readability.
Contact Us