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 - Return from a promise then()

function justTesting(input) {
    return new Promise(function(resolve, reject) {
        // some async operation here
        setTimeout(function() {
            // resolve the promise with some value
            resolve(input + 10);
        }, 500);
    });
}

justTesting(29).then(function(val) {
   // you access the value from the promise here
   log(val);
});

// display output in snippet
function log(x) {
    document.write(x);
}
Comment

Using Then To Create A Promise In JavaScript

async function abc()
{
Promise.resolve("hello world").then(function(value)
{
	console.log(value);
    }
Comment

Create A Promise And Then Return It


let fetchSent =	fetch("/test", {method:"POST", body: JSON.stringify({name:"NAME NAME NAME"}), headers: {'Content-type': 'application/json; charset=UTF-8'}});
const p1 = new Promise((resolve, reject) => {
  resolve(fetchSent);
  // or
  // reject(new Error("Error!"));
})
	 

 return p1;
/*console.log(p1) will yield the fetch promise that was sent to you*/
Comment

js return a promise

function myAsyncFunction(url) {
  return new Promise((resolve, reject) => {
    const xhr = new XMLHttpRequest();
    xhr.open("GET", url);
    xhr.onload = () => resolve(xhr.responseText);
    xhr.onerror = () => reject(xhr.statusText);
    xhr.send();
  });
}
Comment

js function that return a promise example

// ? created a function that returns a promise, take a parameter of a boolean
function myPromise(bool) {
  return new Promise((resolve, reject) => {
    if (bool) {
      resolve("I have succeeded");
    } else {
      reject("I have failed");
    }
  });
}

// ? call the function and pass in a boolean
myPromise(true).then((res) => console.log(res));
myPromise(false).then((res) => console.log(res)).catch((err) => console.log(err));
Comment

PREVIOUS NEXT
Code Example
Javascript :: random between min and max 
Javascript :: html select multiple selected values 
Javascript :: elasticsearch response format 
Javascript :: javascript copy object except one property 
Javascript :: discord.js mobile status 
Javascript :: automated counter with react hooks 
Javascript :: how to set dynamic autocomplete with material ui 
Javascript :: sequelize findall return 
Javascript :: modal example react native 
Javascript :: jquery script cdn stackoverflow 
Javascript :: reactjs svg SyntaxError: unknown: Namespace tags are not supported by default 
Javascript :: javascript convert string to bool 
Javascript :: Find the Longest Word in a String 
Javascript :: react function runs several times 
Javascript :: set proxy for npm 
Javascript :: shopify api for add to cart 
Javascript :: koa access request body 
Javascript :: convert matrix string to matrix javascript 
Javascript :: angular firebase 
Javascript :: how to hide footer in specefic pages in react router 
Javascript :: mongoose get elements that contain substring 
Javascript :: json.parse 
Javascript :: array of objects in javascript short 
Javascript :: what is == in js 
Javascript :: Device detector in react js 
Javascript :: function hoisting in js 
Javascript :: javascript stringify blob 
Javascript :: ckeditor ignore contenteditable 
Javascript :: redis to promise 
Javascript :: vue for start at index 
ADD CONTENT
Topic
Content
Source link
Name
6+6 =