JS Async Always Returns New Promise

A JavaScript async function always returns a new promise, even if you immediately return a promise.

Experimental Setup

1
2
3
4
5
6
7
8
9
$ node -v
v8.4.0
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 16.04.3 LTS
Release: 16.04
Codename: xenial

Experiments

Confirm that a new promise is always generated by an async function

1
2
3
4
5
6
7
// test1.js
const promise = new Promise((resolve,reject)=>resolve());
async function test(){
return promise;
}
console.log(test() !== test());
1
2
$ node test1.js
true

Examine the behavior of the resolve function passed to executor function of a new promise

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// test2.js
const resolvedPromise = new Promise(resolve => resolve('resolved'));
const rejectedPromise = new Promise((_, reject) => reject('rejected'));
function test2(promise){
return new Promise(resolve => {
resolve(promise);
});
}
test2(resolvedPromise).then(v => {
console.log('resolvedPromise:', v);
});
test2(rejectedPromise).catch(v => {
console.log('rejectedPromise:', v);
});
1
2
3
$ node test2.js
resolvedPromise: resolved
rejectedPromise: rejected

Take Away

Working with an async function is equivalent to working within the executor of a new promise.

The resolve function, the first argument of the promise executor, behaves the same as the return statement of an async
function.

1
2
3
4
5
6
function test() {
return new Promise((resolve,reject) => {
// resolve === return
// reject === throw
}
}

References