js -Asynchrononous

//! Asynchrononous javascript

//! synchrononous:

/*

console.log(“First”); //First
console.log(“Second”); //Second
console.log(“Third”); //Third

task contionue vaa poittea irukkum

*/

//! Asynchrononous:

/*

console.log(“Hello World.!”); // 1st print aagum

setTimeout(() => {
console.log(“Hello after 2 seconds”); // 2 sec apram third print agum
}, 2000);

console.log(“welcome ‘javascript'”); // second print aagum

*/

/*

//! callback function

// Callback illustrated

function One() {
// Do something // apram one ulllathu execute agum.
}
function Two(call_One) {
// Do something else
call_One() // one na call pannum
}
Two(One); // two vaa call pannum

//? call back inga aathigam use pannrathu illa.

*/

/*

//! Promises

// !3 states: Pending, Fulfilled, Rejected

// ! Pending: Initial state, not yet started
// ! Fulfilled: Successful, resolved with a value
// ! Rejected: Failed, rejected with a reason

 // var name //obj // 2 arguments 
Enter fullscreen mode
Exit fullscreen mode

const myPromise = new Promise((resolve, reject) => {

 //false
const error = true; // condition if (!error) { resolve("Yes! resolved the promise");
} else { reject("No! rejected the promise.");
}
Enter fullscreen mode
Exit fullscreen mode

})

console.log(myPromise);

//! true output: return states

// Promise : ‘NO! reJECT the promise.’

//! false output: return states

// Promise {: ‘Yes! resolved the promise’
// [[Prototype]]: Promise[[PromiseState]]: “fulfilled”[[PromiseResult]]: “Yes! resolved the promise”

 //function
Enter fullscreen mode
Exit fullscreen mode

myPromise

.then(value =>{

//console.log(value); // ‘Yes! resolved the promise’
return value + ” welcome”; // ‘Yes! resolved the promise welcome’

})

//! another then another then use pannalamm.

.then(newvalue=> {

console.log(newvalue); // ‘Yes! resolved the promise’ return panna value ingaa vaarum

})
.catch(error =>{

console.log(error); // 'No! rejected the promise.'
Enter fullscreen mode
Exit fullscreen mode

});

*/

// !example:

/*

const myPromise = new Promise((resolve, reject) => {

const error = false; if (!error) { resolve("Yes! resolved the promise");
} else { reject("No! rejected the promise.");
}
Enter fullscreen mode
Exit fullscreen mode

});

const myNextPromise = new Promise((resolve, reject) => {

setTimeout(function () { resolve("my next promise resolved"); }, 3000)
Enter fullscreen mode
Exit fullscreen mode

});

myPromise
.then((value) => {
console.log(value); // ‘Yes! resolved the promise’ return panna value ingaa vaarum
})

myNextPromise.then(value=> {

console.log(value); // ‘Yes! resolved the promise’ return panna value ingaa vaarum

}).catch(error =>{

console.log(error); // 'No! rejected the promise.'
Enter fullscreen mode
Exit fullscreen mode

});

*/

//! pending

const users = fetch(“http://jsonplaceholder.typicode.com/users”).then(response =>{

//console.log(response);//Response {type: ‘cors’, url: ‘https://jsonplaceholder.typicode.com/users‘, redirected: true, status: 200, ok: true, …}
return response.json();

}).then(users =>{

console.log(users);
Enter fullscreen mode
Exit fullscreen mode

// console.log(users[0].name); // ‘Leanne Graham’
//console.log(users[0].email); // ‘[email protected]
//console.log(users[0].address.street); // ‘Kulas Light’

//users.forEach(user => { // for each // console.log(users);
Enter fullscreen mode
Exit fullscreen mode

// });

})

console.log(users); // Promise {}

//! Async / Await


Discover more from Coursity

Subscribe to get the latest posts sent to your email.

Leave a Comment

Your email address will not be published. Required fields are marked *

Discover more from Coursity

Subscribe now to keep reading and get access to the full archive.

Continue reading

Scroll to Top