84148

Promise.resolve inside then() method does not pass its resolved value

Question:

I found an interest thing about Promise. When I run the following codes, it gave me the output of 'aa' as oppose to 'bb', which confused me a lot. Does anyone understand why and give a explanation, please? Thanks!

<pre class="snippet-code-js lang-js prettyprint-override">Promise.resolve('aa') .then(Promise.resolve('bb')) .then(console.log);

Answer1:

Well, you're misusing a .then() handler so it is no surprise that you don't get the desired answer.

A .then() handler should be passed a function reference. You are passing it a promise which it dutifully ignores because it's not a callable function.

When you do this:

.then(Promise.resolve('bb'))

That executes Promise.resolve('bb') immediately and passes the return result (which is a promise) to .then(). So, you're passing a promise to .then() when you should be passing a function reference. If you change your code to this, then you will get what you expect:

<pre class="snippet-code-js lang-js prettyprint-override">Promise.resolve('aa') .then(function() {return Promise.resolve('bb');}) .then(console.log);

Remember, the point of passing something to .then() is that it can be executed LATER when the parent promise resolves/rejects. So, for that to be possible, you have to pass a function reference that can be called by the promise infrastructure at some later time.

Using ES6 syntax, you could shorten to this:

Promise.resolve('aa') .then(_ => Promise.resolve('bb)) .then(console.log);

Recommend

  • MD5 Inconsistent between C# and golang
  • How do I get the handle to a comment element in javascript?
  • org.specs2.mock.Mockito matchers are not working as expected
  • Using a indentifier or reserved word in a automation object under FPC
  • Java Multithreading : How to make a thread wait ?
  • Pointer to a function with reduced arguments
  • Is SPLFileObject atomic?
  • Preprocessor-IF doesn't work
  • High quality image re-sampling in Mono/C#/ASP.NET
  • How to export certificate from Windows certificate store via CryptoAPI as Base64 string
  • Recursively defined nested types (in terms of incomplete types)
  • Impossible to pass the router service to a twig extension in symfony 2.6
  • SEO friendly 301 redirect .htm to .aspx
  • VB.Net Double comparison after some additions
  • Zeromq with python hangs if connecting to invalid socket
  • Android onKey w/ virtual keyboard
  • why calling cd shell command through system() or execvp() from a child process won't work?
  • using System.Speech.Synthesis with Windows10 universal app (XAML-C#)
  • How to use jQuery's $.post() method with async/await and typescript
  • Overlapping controls in Windows XP
  • GAE: Way to get reference to an HttpSession from its ID?
  • Spring boot 2.0.0.M4 required a bean named 'entityManagerFactory' that could not be found
  • What is the purpose of TaskExecutor in spring?
  • D3 get axis values on zoom event
  • Detect when Facebook like button is clicked
  • Needing to do .toArray() to get output of mongodb .find() on key name not value
  • AJAX Html Editor Extender upload image appearing blank
  • Make VS2015 use angular-cli ng at build time in a .NET project
  • Setting up SourceTree to merge unity3d scenes with UnityYAMLMerge
  • Unity3D & Android: Difference between “UnityMain” and “main” threads?
  • Can a Chrome extension content script make an jQuery AJAX request for an html file that is itself a
  • Upload files with Ajax and Jquery
  • How to pass list parameters for each object using Spring MVC?
  • AngularJs get employee from factory
  • Proper way to use connect-multiparty with express.js?
  • Exception on Android 4.0 `android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode)`
  • Binding checkboxes to object values in AngularJs
  • Net Present Value in Excel for Grouped Recurring CF
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?