Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

nodejs promise async

// server.js
 
function square(x) {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve(Math.pow(x, 2));
    }, 2000);
  });
}
 
async function layer(x)
{
  const value = await square(x);
  console.log(value);
}
 
layer(10);
Comment

async await for promise nodejs

<script>
  const helperPromise = function () {
    const promise = new Promise(function (resolve, reject) {
      const x = "geeksforgeeks";
      const y = "geeksforgeeks";
      if (x === y) {
        resolve("Strings are same");
      } else {
        reject("Strings are not same");
      }
    });
 
    return promise;
  };
 
  async function demoPromise() {
    try {
      let message = await helperPromise();
      console.log(message);
    } catch (error) {
      console.log("Error: " + error);
    }
  }
 
  demoPromise();
</script>
Comment

async promise javascript

await new Promise(resolve => setTimeout(resolve, 1000))
Comment

example of promise and async in javascript

// example of promise and async in javascript
// 1) promise: we need to write new keyword to create promise
function withConstructor(num){
  return new Promise((resolve, reject) => {
    if (num === 0){
      resolve('zero');
    } else {
      resolve('not zero');
    }
  });
}

withConstructor(0)
  .then((resolveValue) => {
  console.log(` withConstructor(0) returned a promise which resolved to: ${resolveValue}.`);
});
// Output: withConstructor(0) returned a promise which resolved to: zero.

// 2) async:  we don't need to write new keyword to create promise
async function withAsync(num){
  if (num === 0) {
    return 'zero';
  } else {
    return 'not zero';
  }
}

withAsync(100)
  .then((resolveValue) => {
  console.log(` withAsync(100) returned a promise which resolved to: ${resolveValue}.`);
})

// Output: withAsync(100) returned a promise which resolved to: not zero.
Comment

promise async await

async function myFetch() {
  let response = await fetch('coffee.jpg');
  if (!response.ok) {
    throw new Error(`HTTP error! status: ${response.status}`);
  }
  return await response.blob();

}

myFetch().then((blob) => {
  let objectURL = URL.createObjectURL(blob);
  let image = document.createElement('img');
  image.src = objectURL;
  document.body.appendChild(image);
}).catch(e => console.log(e));
Comment

javascript promise async

await new Promise(resolve => setTimeout(resolve, 5000));
Comment

Javascript async await & Promise

we can only use await keyword before a function that

returns either a promise or is itself async.

Note: we can use async function or function returning
a Promise with .then()
Comment

nodejs promise async

// Normal Function
function add(a,b){
  return a + b;
}
// Async Function
async function add(a,b){
  return a + b;
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: regex to get part of word nodejs 
Javascript :: fix your timestep javascript 
Javascript :: javascript requestanimationframe stack overflow 
Javascript :: get minutes with 2 numbers 
Javascript :: returned data has p tags react 
Javascript :: react native whatsapp integration 
Javascript :: KeyEvent event = new KeyEvent(k); event.call(); 
Javascript :: set up background process in express app 
Javascript :: zeamster examples react node 
Javascript :: Javascript Make your console talk! 
Javascript :: keyup.enter will work in mac 
Javascript :: cordova read file in www folder 
Javascript :: automatic jquery interceptor with token 
Javascript :: how to show product count in jquery return response 
Javascript :: variable is not null if used optstring json object 
Javascript :: how to change grid size with conditional expression in react 
Javascript :: add object to array setstate 
Javascript :: xdebug in blade 
Javascript :: function resizeBase64Img(base64, newWidth, newHeight) { return new Promise<string((resolve, reject)={ 
Javascript :: vuejs my chart load before fetch data 
Javascript :: cannot create an instance of an abstract class httphandler angular 
Javascript :: difference between Redis and StrictRedis 
Javascript :: jquery post data into an iframe from textarea live 
Javascript :: get the latest git commit SHA-1 in a repository js 
Javascript :: js map vs react js map 
Javascript :: Node Locking 
Javascript :: in javascript advertising on website only for 5 seconds 
Javascript :: asp.net run javascript on page load 
Javascript :: validate url javascript 
Javascript :: firestore return the content of an aarray Nodejs 
ADD CONTENT
Topic
Content
Source link
Name
2+8 =