Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Checking equality with Promise.resolve vs async return

/* Even though the return value of an async function behaves as if it's wrapped in a
Promise.resolve, they are not equivalent. */

//For example, the following:
async function foo() {
   return 1
}

//...is similar to:
function foo() {
   return Promise.resolve(1)
}

/*An async function will return a different reference, whereas Promise.resolve returns the
same reference if the given value is a promise.*/
const p = new Promise((res, rej) => {
  res(1);
})

async function asyncReturn() {
  return p;
}

function basicReturn() {
  return Promise.resolve(p);
}

console.log(p === basicReturn()); // true
console.log(p === asyncReturn()); // false
Comment

PREVIOUS NEXT
Code Example
Javascript :: how to get button text in javascript 
Javascript :: how to create a mixed array in javascript 
Javascript :: encryption decryption in javascript 
Javascript :: discord.js delete commend after reply 
Javascript :: replicate component did update hooks 
Javascript :: how to pass data in router link react-typescript 
Javascript :: fecha javascript mes de 2 digitos 
Javascript :: style react native alert text 
Javascript :: inline default export arrow in js 
Javascript :: three js div over orbitcontrol 
Javascript :: how to convert variable to string in jquery 
Javascript :: lity popup 
Javascript :: (Math.floor(Math.random() * 90000) + 10000).toString(); 
Javascript :: clima 
Javascript :: import image in react js 
Javascript :: email id validation in javascript 
Javascript :: eact redux createSlice or createReducer 
Javascript :: react-chartjs-2 donut chart 
Javascript :: navigate to another page on vue js button 
Javascript :: Grunt--project configuration object--uglify 
Javascript :: find date range btween start date to end date in node js 
Javascript :: reactvs y axis range 
Javascript :: how to show numbers in javascript using while loop 
Javascript :: negate expression prolog 
Javascript :: NetSuite Add Line Item to a Sales Order in afterSubmit 
Javascript :: javascript array same class elements 
Javascript :: xslt 2 node text replace string 
Javascript :: Mongoose make Object required 
Javascript :: How to subscribe changes to property of a directive 
Javascript :: JavaScript Program to illustrate split() function 
ADD CONTENT
Topic
Content
Source link
Name
9+3 =