Why does the Promise constructor require a function that calls 'resolve' when complete, but 'then' does not - it returns a value instead?

后端 未结 4 1946
说谎
说谎 2020-12-04 09:11

As I plunge into studying Promises, my understanding has halted on the following question that I do not find discussed (all I find are specific discussions of t

4条回答
  •  悲&欢浪女
    2020-12-04 09:57

    The whole point of the promise constructor executor function is to disseminate resolve and reject functions to non-promise-using code, to wrap it and convert it to use a promise. If you wanted to limit this to synchronous functions only, then yes, a return value from the function could have been used instead, but that would have been silly since the useful part is to disseminate the resolver and reject functions to code that actually runs later (way after the return), e.g. to callbacks passed in to some asynchronous API.

提交回复
热议问题