Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript return promise

function doSomething() {
  return new Promise((resolve, reject) => {
    console.log("It is done.");
    // Succeed half of the time.
    if (Math.random() > .5) {
      resolve("SUCCESS")
    } else {
      reject("FAILURE")
    }
  })
}

const promise = doSomething(); 
promise.then(successCallback, failureCallback);
Comment

How to access return value of promise

const address = fetch("https://jsonplaceholder.typicode.com/users/1")
  .then((response) => response.json())
  .then((user) => {
    return user.address;
  });

const printAddress = async () => {
  const a = await address;
  console.log(a);
};

printAddress();
Comment

return promise in node js

module.exports.getAllPosts = function()
{
    return new Promise((resolve,reject)=>{

        if(posts == true)
        {
          resolve("success");
        }else{
        reject("Failed");
        }
    });
}
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

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 :: on click jquery 
Javascript :: async await useeffect react 
Javascript :: javascript get typeof array 
Javascript :: import applymiddleware 
Javascript :: sum all elements in array javascript 
Javascript :: Without using a new array or the reverse() method to Reverse an Array 
Javascript :: how to use bootstrap icons in react components 
Javascript :: validate phone number regex 
Javascript :: python iterate json file 
Javascript :: javascript get html slider value 
Javascript :: install nodemon 
Javascript :: TypeError: this.authenticate is not a function 
Javascript :: how to display api data in html 
Javascript :: js input type range get value while sliding 
Javascript :: how to navigate programatically in class component react router v6 
Javascript :: first n even numbers sum javascript 
Javascript :: array of objects to array 
Javascript :: how to find all permutations of an array with javascript 
Javascript :: datepicker auto close 
Javascript :: disable global require eslint 
Javascript :: js map constructor 
Javascript :: how to call web api with the useeffect hook in react 
Javascript :: remove array item from localStorage 
Javascript :: javascript object tostring 
Javascript :: remove duplicate object from array javascript 
Javascript :: how to hide button in react 
Javascript :: get text from selected select javascript object 
Javascript :: esversion 9 
Javascript :: ajax form picture upload 
Javascript :: antd modal hide ok button 
ADD CONTENT
Topic
Content
Source link
Name
9+4 =