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

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

PREVIOUS NEXT
Code Example
Javascript :: add attribute in select option 
Javascript :: jquery upload image 
Javascript :: flatten an array without using .flat(); 
Javascript :: react function being called every minute 
Javascript :: moment timezone set default timezone 
Javascript :: how to make link in discord.js 
Javascript :: fixed menu reactjs 
Javascript :: javascript string to lowercase 
Javascript :: basic express graphql 
Javascript :: react input number validation 
Javascript :: javascript if browser out of focus 
Javascript :: distance to km javascript 
Javascript :: math floor 
Javascript :: chrome extension open new tab from popup 
Javascript :: get all the child of the same class javascript 
Javascript :: expo custom fonts 
Javascript :: javascript Using debugger 
Javascript :: how to add custom font to react project 
Javascript :: how to show 1 to 10 odd numbers in javascript 
Javascript :: Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "text/html" 
Javascript :: express receive post 
Javascript :: array to object 
Javascript :: jquery display modal bs4 
Javascript :: copy to clipboard 
Javascript :: random number in javascript between two numbers 
Javascript :: javascript browser tab active 
Javascript :: display amount with currency for jquery 
Javascript :: Radom String in Javascript 
Javascript :: how to ssh into gke node 
Javascript :: jquery .click function call 
ADD CONTENT
Topic
Content
Source link
Name
7+1 =