1

Would the function that is registered/executed by the then method be able to catch hold of its own resolve?

A promise when its constructed is able to inject its resolve method like:

new Promise(function(resolve, reject){ async(resolve();})

However the action registered by then normally is triggered in resolve method like (at least in some examples I have seen):

resolve = function(value){
  self.value = value;
  self.state = "resolved";
  deferred.resolve(deferred.transform(self.value));
}

There is no real means to inject deferred.resolve into deferred.transform method. It almost as if the then doesn't expect the function it would execute to be asynchronous unlike like a Promise Constructor and hence it isn't isomorphic?

In Javascript Promise Sequence you actually create 8 Promises for a job that should ideally be done in 4 Promises. Can't this be solved if the transformation function registered by then can get hold of its own resolve which is the deferred.resolve? The transformation function can be ansychronous and it can resolve it when it's done.

Community
  • 1
  • 1
Nishant
  • 20,354
  • 18
  • 69
  • 101
  • 1
    What is `deferred` in your case? (ES6 promises have no `deferred` variable). – nils Mar 04 '16 at 15:23
  • `deferred` is a placeholder in the original Promise to store the `function` registered by the then and the `resolve` to the promise returned so that it could initiated at a later stage when the original Promise is resolved. However I need to see if ES6 specifices it or else I could make this a generic question? – Nishant Mar 04 '16 at 15:30
  • 1
    Which answer in the linked post are you exactly referring to (that you think uses too many promises)? Keep in mind that promises are cheap. – Bergi Mar 04 '16 at 15:40
  • The code in the original question itself. Not the answers. I am just wondering if my first asyn function executed using Promise Contructor can get hold of resolve, shouldn't function that is registered/executed by then also be able to get that resolve instaed of only value which can be limiting? – Nishant Mar 04 '16 at 15:43

1 Answers1

2

Would the function that is registered/executed by the then method be able to catch hold of its own resolve?

No.

Or at least not without returning a thenable that intercepts its callback, but that doesn't really make sense.

It almost as if the then doesn't expect the function it would execute to be asynchronous

Oh, it does. Your misunderstanding seems to be based on thinking that asynchronous functions are those that take callbacks. They aren't. Asynchronous functions are those that return promises! And then is totally capable dealing with them.

…unlike the Promise Constructor

It's the other way round. The Promise constructor is the odd one out here. Its only purpose is to transform old-style callback-taking functions to promises. You're expected to deal with fully promisified functions only these days, and new APIs will return promises right away.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • Ok thanks really enlightening. You seem to mean that XMLHttpRequest and setTimeout are not correct in the first place! Something for me to understand better. – Nishant Mar 04 '16 at 16:03
  • "Its only purpose is to transform old-style callback-taking functions to promises" Do you mean that Promise constructor is like a legacy support API? Or is it like one can't get around passing out the very first "resolve"? Ofcourse you could only deal with API's only but the very first Promise that is ever constructed (not excluding the ones created by a library internally) needs to pass its resolve outside for some event to respond back? – Nishant Mar 05 '16 at 17:51
  • 1
    Yes, all the new APIs are based on streams and promises directly, you can get promises for events to respond. `new Promise` is only necessary to construct promises on the low level (e.g. inside of `then`) or inside of libraries. – Bergi Mar 05 '16 at 19:25