checking equality with promise resolve vs async return

Solutions on MaxInterview for checking equality with promise resolve vs async return by the best coders in the world

showing results for - "checking equality with promise resolve vs async return"
Valentina
15 Jan 2020
1/* Even though the return value of an async function behaves as if it's wrapped in a
2Promise.resolve, they are not equivalent. */
3
4//For example, the following:
5async function foo() {
6   return 1
7}
8
9//...is similar to:
10function foo() {
11   return Promise.resolve(1)
12}
13
14/*An async function will return a different reference, whereas Promise.resolve returns the
15same reference if the given value is a promise.*/
16const p = new Promise((res, rej) => {
17  res(1);
18})
19
20async function asyncReturn() {
21  return p;
22}
23
24function basicReturn() {
25  return Promise.resolve(p);
26}
27
28console.log(p === basicReturn()); // true
29console.log(p === asyncReturn()); // false