Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

new promise function

const myPromise = new Promise((resolve, reject) => {
  setTimeout(() => {
    resolve('foo');
  }, 300);
});

myPromise
  .then(handleResolvedA, handleRejectedA)
  .then(handleResolvedB, handleRejectedB)
  .then(handleResolvedC, handleRejectedC);
Comment

promise in javascript

const studentRol=new Promise((resolve, reject) => {
    setTimeout(()=>{
        /**this function will give the value Of tageted studebt Roll number  */
           let RollOffStd=[1,2,3,4,5,6,7,8];
        
            for (let index = RollOffStd[RollOffStd.length-1]+1 ; index <50; index++) {
                RollOffStd.push(index)
                
            }
            
            resolve( RollOffStd)
            
            reject('My name is Noor mohammad ')
      

    },1000)
})


const mybiodata=(gRollOfStudent /* this is First parameter OF ( mybiodata function ) and You can change parameter value  */)=>{
    return new Promise((resolve, reject) => {
        setTimeout((x) => {
           let  bio={
                myname : 'Noor mohammad Patwary ' ,
                age : 25 ,
            }

            resolve(`my name is ${bio.myname } and my age =  ${bio.age } and my roll is =${x} ` )
        }, 1000,gRollOfStudent);
    })
}

studentRol.then(( RollOfStudent)=>{
    console.log(RollOfStudent); /** From here we are gating the Value OF student roll number  */
    mybiodata(RollOfStudent[1] /* this is First Argument OF ( mybiodata function )*/).then((fff)=>{
        console.log(fff);
    })

}).catch((x)=>{
    console.log(x);
})



Comment

promise in javascript

/*
A promise is a building object of JavaScript, using it we can easily do 
asynchronous tasks. Also, the concept that is used to create clean code 
basically promises.
*/
//Promise
let firstPromise = new Promise((resolved, reject) => {
    let fullName = 'Muhammad Shahnewaz';
    setTimeout(() => resolved(fullName), 3000);  //we need to use setTimeout() 
}).then((name) => {
    console.log('I am ' + name);    //Muhammad Shahnewaz
});
Comment

Promise in ES6

function makeRequest(method, url) {
  return new Promise((resolve, reject) => {
    let request = new XMLHttpRequest()

    request.open(method, url)
    request.onload = resolve
    request.onerror = reject
    request.send()
  })
}

makeRequest('GET', 'https://url.json')
  .then((event) => {
    console.log(event.target.response)
  })
  .catch((err) => {
    throw new Error(err)
  })
Comment

Promises in ES6

function asyncFunc() {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
          const result = Math.random();
          result > 0.5 ? resolve(result) : reject('Oppps....I cannot calculate')
        }, 1)
    });
}

for (let i=0; i<10; i++) {
	asyncFunc()
    	.then(result => console.log('Result is: ' + result))
    	.catch(result => console.log('Error: ' + result))
}
Comment

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));
Comment

js promises

let examplePromise = new Promise(function (resolve, reject) {
  let sum;
  setTimeout(function(){
    sum = 5 + 6;
    if(sum > 10) {
      resolve(sum);
    }else{
      reject('The promise has been rejected');
    }     
  }, 2000);
});
Comment

making promises in js

getData()
    .then(data => console.log(data))
    .catch(error => console.log(error));
Comment

promises in javascript

myPromise.then(
  function(value) { /* code if successful */ },
  function(error) { /* code if some error */ }
);
Comment

promise in js

myPromise
.then(handleResolvedA)
.then(handleResolvedB)
.then(handleResolvedC)
.catch(handleRejectedAny)
.finally(handleComplition)
Comment

javascript promises

let dataIsLoading = true;

promise
  .then(data => {
    // do something with data
  })
  .catch(error => {
   // do something with error
  })
  .finally(() => {
    dataIsLoading = false;
  })
Comment

JavaScript Promises

// returns a promise
let countValue = new Promise(function (resolve, reject) {
   reject('Promise rejected'); 
});

// executes when promise is resolved successfully
countValue.then(
    function successValue(result) {
        console.log(result); // Promise resolved
    },
 )
Comment

PREVIOUS NEXT
Code Example
Javascript :: upload multiple image using jquery 
Javascript :: get contents between tags javascript 
Javascript :: less than equal to in javascript 
Javascript :: responsive font size react native 
Javascript :: react-multi-carousel infinite loop causing issue 
Javascript :: comparing html text by using jquery 
Javascript :: change bg-color all class 
Javascript :: create own rules jquery 
Javascript :: json remove &#34 
Javascript :: jquery get data attribute value by class 
Javascript :: react features 
Javascript :: how to filter multiple values from a json api 
Javascript :: create auto increment mongodb mongoose 
Javascript :: password regex javascript 
Javascript :: nodejs sqlite create db if not exists 
Javascript :: how to put submit type of input element in a queryselector in javascript 
Javascript :: sum up all the first and last digit of a number until only two digits are left 
Javascript :: call by value and call by reference in javascript 
Javascript :: ajax post request javascript 
Javascript :: how to draw a flower in javascript 
Javascript :: javascript use class without instantiating 
Javascript :: jquery table header agnostic of scroll 
Javascript :: how to edit a fil with vanilla js 
Javascript :: elixir guards 
Javascript :: javascript create class 
Javascript :: bitcoin prices in javascript 
Javascript :: react setstate concat string 
Javascript :: vue js filter 
Javascript :: convert json to 2d array 
Javascript :: axios.create 
ADD CONTENT
Topic
Content
Source link
Name
1+2 =