I have a function that returns a promise object. This is my code
var foo = function(){
// doSomething() is a promise object
return doSomething().then(() => {
Promise.resolve('Hello');
});
};
foo().then((res) => {
console.log(res);
// res = undefined but not "Hello"
});
I thought function foo would return the promise object and I would get the string "Hello". But I get undefined. Why?
returnbeforePromise.resolve. However, actually you don't need that at all, and could just return the stringHello. You could make it even simpler by using the concise body form of arrow function, leaving off the{}, and just say.then(() => 'Hello').doSomethingmethod?doSomethingis by definition irrelevant to the behavior of this code, as long as it returns a promise as the OP has already stated.