Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can you assist me in satisfying promises aplus spec 2.2.4 in NodeJS? #217

Open
danday74 opened this issue Nov 10, 2015 · 4 comments
Open

Comments

@danday74
Copy link

I am writing an HTTP Promises package for nodeJS. I need it to be promises aplus compliant. I am using promises-aplus-tests - https://www.npmjs.com/package/promises-aplus-tests

I have created an adapter as follows ..

var adapter = {
    deferred: function() {
        var d = {}
        d.promise = new HTTPromise(new GETOptions('/path', DEFAULT_OPTIONS));
        d.resolve = executorResponseFunc;
        d.reject = executorRejectFunc;        
        return d;
    }
}

promisesAplusTests(adapter, function (err) {
    done(err);
});

My tests are failin on https://promisesaplus.com/#point-34 ... with further help on https://promisesaplus.com/#point-67

2.2.4 onFulfilled or onRejected must not be called until the execution context stack contains only platform code

3.1 Here “platform code” means engine, environment, and promise implementation code. In practice, this requirement ensures that onFulfilled and onRejected execute asynchronously, after the event loop turn in which then is called, and with a fresh stack. This can be implemented with either a “macro-task” mechanism such as setTimeout or setImmediate, or with a “micro-task” mechanism such as MutationObserver or process.nextTick. Since the promise implementation is considered platform code, it may itself contain a task-scheduling queue or “trampoline” in which the handlers are called.

But I can't get it passing this promises aplus test. I am using the bog standard JavaScript Promises library (which on its own passes the tests) documented at ...

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise

new Promise(executor);

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

In the executor I have tried ...

if (response.statusCode === 200) {
    process.nextTick(() => {
        resolve(finalResponse);   
    });
} else {
    process.nextTick(() => {
        reject(finalResponse);
    });
}

and also ...

process.nextTick(() => {
    if (response.statusCode === 200) {
        resolve(finalResponse);      
    } else {
         reject(finalResponse);
    }
});

There seems to be a lot of talk on the Internet but not many code examples of how to comply. Can anyone provide a code sample please on how to satisfy this test and how you would use nextTick or another suitable solution?

Worthing mentioning I have another call to reject within an HTTP error handler within the executor function;

Many thanks

@ForbesLindesay
Copy link
Member

You don't seem to have provided the code for your entire adapter (e.g. where is executorResponseFunc defined?) so it's very difficult to know what's going on here. Ideally, provide us all the code and then maybe someone can have a quick look.

@danday74
Copy link
Author

Hi - sorry my thinking was wrong
http://stackoverflow.com/questions/33629004/can-you-assist-me-in-satisfying-promises-aplus-spec-2-2-4-in-nodejs
Thanks for your help :)

On Tue, Nov 10, 2015 at 4:25 PM, Forbes Lindesay [email protected]
wrote:

You don't seem to have provided the code for your entire adapter (e.g.
where is executorResponseFunc defined?) so it's very difficult to know
what's going on here. Ideally, provide us all the code and then maybe
someone can have a quick look.


Reply to this email directly or view it on GitHub
#217 (comment)
.

@ForbesLindesay
Copy link
Member

You should never return from a constructor. If you need to do inheritance (you almost certainly don't) then use extends and super rather than returning from the constructor. If you don't need inheritance (again, I'm pretty sure you don't), just use functions instead of classes. It looks like your code could simplify to just

function HTTPromise(options) {
    ... blah blah blah
    let executor = THE_REAL_EXECUTOR;
    return new Promise(executor);
}

P.S. it sounds like what you're doing is very similar to then-request which has implementations for both browser and server side. It demonstrates how to write a function that returns a promise and how to extend that promise with an extra method (.getBody).

@danday74
Copy link
Author

thanks i appreciate your input
i know you should never return a promise from a constructor but on this
occassion we discussed it and have very valid reasons for doing so, won't
bore you with them - thanks ever so much for your help :)

On Tue, Nov 10, 2015 at 6:14 PM, Forbes Lindesay [email protected]
wrote:

You should never return from a constructor. If you need to do inheritance
(you almost certainly don't) then use extends and super rather than
returning from the constructor. If you don't need inheritance (again, I'm
pretty sure you don't), just use functions instead of classes. It looks
like your code could simplify to just

function HTTPromise(options) {
... blah blah blah
let executor = THE_REAL_EXECUTOR;
return new Promise(executor);
}

P.S. it sounds like what you're doing is very similar to then-request
https://github.com/then/then-request which has implementations for both
browser and server side. It demonstrates how to write a function that
returns a promise and how to extend that promise with an extra method (
.getBody).


Reply to this email directly or view it on GitHub
#217 (comment)
.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants