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

promise syntax in js


// make new promise ... so this object will promise us that it will hold an Asynchronous action
// some times promise resolve and some times promise get rejected
const A = new Promise((resolve, reject) => {
    setTimeout(() => {
        // here we pass argument if promise get resolved
        resolve('Done');

        // here we pass argument if promise get rejected
        reject('Didn"t');
    }, 3000);
});


// design handling callback function for resolve 
let handleResolvedA = (massage)=>{console.log(massage)}

// design handling callback function for reject 
let handleRejectedA = (massage)=>{console.log(massage)}


// do handling for both
A.then(handleResolvedA, handleRejectedA)
Comment

promise in js

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

PREVIOUS NEXT
Code Example
Javascript :: react native cli sdk.dir 
Javascript :: nan in js 
Javascript :: Capitalize word 
Javascript :: reduce in javascript 
Javascript :: if else function react native 
Javascript :: drupal 8 webform insert node twig value 
Javascript :: javascript line chart 
Javascript :: react native position 
Javascript :: listen to localstorage changes 
Javascript :: javascript recursion 
Javascript :: events in javascript 
Javascript :: react native bottom sheet above the bottom menu 
Javascript :: js on highlight 
Javascript :: console log on html 
Javascript :: add class to new element javascript 
Javascript :: js get dropdown value 
Javascript :: nodemon exclude 
Javascript :: create javascript set 
Javascript :: javascript Rename in the import file 
Javascript :: trigger keydown event javascript 
Javascript :: node js classes 
Javascript :: how to disable option after select using jquery 
Javascript :: upload file angular rest api 
Javascript :: blockchain javascript 
Javascript :: angular 14 new features 
Javascript :: nodelist to array 
Javascript :: typescript deserialize json 
Javascript :: arrow functions syntax 
Javascript :: Get the Timezone 
Javascript :: react native image picker 
ADD CONTENT
Topic
Content
Source link
Name
3+3 =