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 :: react js if statement 
Javascript :: javascript filter method arrow function 
Javascript :: webpack dev server 
Javascript :: svg clientx 
Javascript :: js pop matched value in array 
Javascript :: remove last tag in dom javascript 
Javascript :: react native image from web 
Javascript :: code javascript 
Javascript :: use effect hook 
Javascript :: write hello world in javaskript 
Javascript :: payfast javascript 
Javascript :: react fetch data in for loop 
Javascript :: iterate over array of object javascript and access the properties 
Javascript :: how to use react-native-vector-icons 
Javascript :: tailwind rn yarn install 
Javascript :: how to search and filter js 
Javascript :: re init data vue js 
Javascript :: next js get query parameters 
Javascript :: chrome.browseraction.getbadgetext 
Javascript :: click binding angular 8 
Javascript :: How to add click event to table row js 
Javascript :: anagram javascript 
Javascript :: how to extract text from image using javascript 
Javascript :: useParams 
Javascript :: trim text 
Javascript :: how to open a tcp connection in javascript 
Javascript :: angularjs ng-options name value 
Javascript :: how to make a div auto refresh js 
Javascript :: javscript loop array 
Javascript :: check if something is a letter in js 
ADD CONTENT
Topic
Content
Source link
Name
4+7 =