Answers for "Checking equality with Promise.resolve vs async return"

0

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
Posted by: Guest on June-07-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language