📜  使用 Promise.resolve 与异步返回检查相等性 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:03:18.642000             🧑  作者: Mango

代码示例1
/* 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