Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return value of Promise's resolve/reject functions

Consider this situation.

new Promise(function(resolve, reject) {
    var x = resolve(2);
});

What value will x be? I tried to print it and it showed me undefined. It is intuitive, but is it always so? Is it in docs?

Second question

new Promise(function(resolve, reject) {
    resolve(2);
    return 5;
});

What should we return from the function that we put into a promise? Is this value ignored?

like image 588
Michał Kownacki Avatar asked Jul 27 '15 11:07

Michał Kownacki


Video Answer


1 Answers

The return value of the promise constructor is ignored.

The resolve function also returns undefined.

This was first specified in the promise constructor spec and later in the ES2015 (ES6) language specification.

like image 172
Benjamin Gruenbaum Avatar answered Nov 17 '22 02:11

Benjamin Gruenbaum