The problem were facing:
return
(new Promise(..)) //the promise we want to return
.then(()=>undefined) // the promise were actually returning, which resolves to undefined
As you may already noticed, then returns a new promise. This has a good reason, it makes promise chaining easy, e.g:
getUser()//an asynchronous action
.then(user=>login(user))//then if we get the user,promise to log in
.then(token=>console.log("logged in,token is "+token) //then if we logged in, log it
.catch(error=>"login failed");//catch all errors from above
But this also creates the small trap, we are facing. The solution could be returning the original promise and not the new promise automatically returned by .then() as this is resolved to undefined as the function inside then is not explicitly returning something:
//what were doing:
Promise.resolve(n*10)//the original promise resolves to n*10
.then(a=>undefined)//the then gets n*10 passed as a, but returns undefined
.then(b=>console.log(b));//b will be undefined :0
//what we want:
var promise=Promise.resolve(n*10);
promise.then(a=>undefined);//a is n*10, this resolves to undefined
promise.then(b=>console.log(b));//but this still logs n*10, as its the original promise :)
So as you can see, to return the original promise, we simply store it in a variable, then assign a .then handler to it, and have still a reference to the original promise which we can assign other handlers to ( or return ).
In action:
function doStuff(n /* `n` is expected to be a number */) {
//create a new promise and store it
var promise=new Promise(function(resolve, reject) {
setTimeout(function() {
resolve(n * 10)
},1000);
});
//add a then handler to this promise
promise.then(result=>console.log(result + " is "+result<100?"not":""+" greater than 100"));
//return the original one
return promise;
}
doStuff(9).then(function(data) {
console.log(data) //not undefined, as original promise
})