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

javascript promise example basic

const anotherPromise = new Promise((resolve, reject) => {
  setTimeout(() => {
    resolve('this is the eventual value the promise will return');
  }, 300);
});

// CONTINUATION
anotherPromise
.then(value => { console.log(value) })
Comment

javascript Program with a Promise

const count = true;

let countValue = new Promise(function (resolve, reject) {
    if (count) {
        resolve("There is a count value.");
    } else {
        reject("There is no count value");
    }
});

console.log(countValue);
Comment

Create A Promise In JavaScript

async function abc() 
{ 	
	const myPromise = new Promise(function(resolve, reject) {
   resolve("hello world");
});
let y= await myPromise;
console.log(y);
/*"hello world*/
	}
Comment

promise syntax for javascript

let myPromise = new Promise(function(myResolve, myReject) {
// "Producing Code" (May take some time)

  myResolve(); // when successful
  myReject();  // when error
});

// "Consuming Code" (Must wait for a fulfilled Promise)
myPromise.then(
  function(value) { /* code if successful */ },
  function(error) { /* code if some error */ }
);
Comment

javascript Create a Promise

let promise = new Promise(function(resolve, reject){
     //do something
});
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

simple promise

getSomethingWithPromise()
  .then(data => {/* do something with data */})
  .catch(err => {/* handle the error */})
Comment

Simplest Promise Example

let hw = await new Promise((resolve)=>{
resolve(“HELLO WORLD”);

})

console.log(hw);
 
/*basically in resolve(whatever') the whatever is returned*/
/*hw will console.log out "HELLO WORLD" */
Comment

making promises in js

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

simple promise

// jeffBuysCake is a promise
const promise = jeffBuysCake('black forest')
Comment

javascript promise example

/* Testing Grepper
Comment

js promise example

function examplePromise(){
  let promiseToReturn = 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);
  });
  return promiseToReturn;
}

console.log('some piece of code');
examplePromise().then(function(result){
  console.log(result);
}).catch(function(error){
  console.log(error);
});
console.log('another piece of code');
Comment

JavaScript Promise Methods

Method	Description
all(iterable)	Waits for all promises to be resolved or any one to be rejected
allSettled(iterable)	Waits until all promises are either resolved or rejected
any(iterable)	Returns the promise value as soon as any one of the promises is fulfilled
race(iterable)	Wait until any of the promises is resolved or rejected
reject(reason)	Returns a new Promise object that is rejected for the given reason
resolve(value)	Returns a new Promise object that is resolved with the given value
catch()	Appends the rejection handler callback
then()	Appends the resolved handler callback
finally()	Appends a handler to the promise
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript return string 
Javascript :: loop through json array python 
Javascript :: how to change html element in javascript 
Javascript :: js get all arguments from function 
Javascript :: toastandroid react native 
Javascript :: override important css 
Javascript :: concat array javascript 
Javascript :: vue localstore 
Javascript :: react alice carousel 
Javascript :: Invariant Violation: "main" has not been registered. This can happen if: * Metro (the local dev server) is run from the wrong folder. Check if Metro is running, stop it and restart it in the current project. 
Javascript :: ws.browser regeneratorRuntime is not defined angular 
Javascript :: js remove specific item from array 
Javascript :: how to redirect react router from the app components 
Javascript :: javascript variable 
Javascript :: JS stack array backwards 
Javascript :: sort function in react js 
Javascript :: react native prevent rotation of screen 
Javascript :: jsx loop array 
Javascript :: jquery get custom attribute 
Javascript :: save networkx graph to json 
Javascript :: discord.js clear console 
Javascript :: angular implementing Validator 
Javascript :: bcrypt 
Javascript :: backbone js cdn 
Javascript :: how to use cookies in react js 
Javascript :: convert json object to lowercase 
Javascript :: nodejs return value 
Javascript :: js concatenate regex 
Javascript :: javascript add days 
Javascript :: sort numbers in javascript 
ADD CONTENT
Topic
Content
Source link
Name
3+4 =