For example, I have pretty current versions of official Joyent NodeJS installed with of course the Google V8 JavaScript engine and also JXcore with the Mozilla SpiderMonkey JavaScript engine.
The former seems to come with native Promises support, the latter seems not to.
(And of course these are not the only two possible environments nodeJS code might run under.
How can my code test its environment to see whether it can use native Promises or not?
It's OK if it detects a proper polyfill as "native" in this case. But it shouldn't detect something like Q as native promises. (Or let me know if I'm wrong about this.)
Well, you can use a package that uses native promises if they're available and if they're not it shims them.
Alternatively, since it's the server, you might be better off using a compatible userland library until all your environments support native promises.
Note that jxcore does not support some standard NodeJS promise features like
unhandledRejectionthat userland polyfills and libraries support.As for detecting native promises - it can be hard across environments. Technically you could check if there is a global
Promiseobject as the first line of code in your program but that's messy compared to the alternative of just using a compatible faster userland library.