promise definition in javascript
A promise is an object that may produce a single value sometime in the future: either a resolved value, or a reason that it's not resolved (e.g., a network error occurred)
promise definition in javascript
A promise is an object that may produce a single value sometime in the future: either a resolved value, or a reason that it's not resolved (e.g., a network error occurred)
what is promise in javascript
Promises make async javascript easier as they are easy to use and write than callbacks. Basically , promise is just an object , that gives us either success of async opertion or failue of async operations
making promises in js
getData()
.then(data => console.log(data))
.catch(error => console.log(error));
javascript promises
var posts = [
{name:"Mark42",message:"Nice to meet you"},
{name:"Veronica",message:"I'm everywhere"}
];
function Create_Post(){
setTimeout(() => {
posts.forEach((item) => {
console.log(`${item.name} --- ${item.message}`);
});
},1000);
}
function New_Post(add_new_data){
return new Promise((resolve, reject) => {
setTimeout(() => {
posts.push(add_new_data);
var error = false;
if(error){
reject("Something wrong in </>, Try setting me TRUE and check in console");
}
else{
resolve();
}
},2000);
})
}
New_Post({name:"War Machine",message:"I'm here to protect"})
.then(Create_Post)
.catch(err => console.log(err));
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us