question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Promise polyfill should be injectable by loaders, instead of relying on global scope pollution

See original GitHub issue

Most.js relies on ES6 Promises but does not offer a way to import a polyfill for them cleanly without polluting the global scope. This is important in particular when you are writing code that should integrate with third parties within one browser context.

For AMD loaders this can be fixed by having Most.js resolve the global Promise through a module. A "most/promise" module could be defined as simply as

define([], function() { return Promise });

This module could then at a user’s discretion be remapped using AMD map configuration to their own, user-defined module that can in turn provide a polyfill for Most.js to use, without requiring the user to pollute the global browser environment with a polyfill implementation written into window.Promise.

Surely other loaders and bundlers support similar extension points.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:16 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
unscriptablecommented, Apr 26, 2017

Hey @rjgotten,

It also continues to write itself into the global scope, so it doesn’t solve the original problem either.

Hmmm… I don’t think it does. I didn’t test the resulting code, but it certainly looks like it doesn’t pollute the global scope to me. Unless I’m missing something, it looks like it creates a module-scoped Promise instance, and since most.js doesn’t specifically look in the global scope, it uses it. No???

Thanks, but that’s not what I meant. You picked one Promise polyfill and baked it directly into the library, which means users no longer have a choice of which polyfill to use.

Personally, I think this is the best way to allow people to use a local implementation of a normally global object. If Promises were just another third-party utility, then I’d be 100% agreeing with you. However, they’re equivalent to Array, RegExp, etc. now.

I know it kinda sucks dealing with browsers, but library authors need to work towards standards and the future. 😃

1reaction
axefrogcommented, Apr 26, 2017

I don’t understand why it’s considered “pollution” to have Promise in the global scope. Why is that polluting the global scope, but all other standard native types are not pollution? (Map, Set, setTimeout, etc.) Promise is found in the global scope in every modern JavaScript VM. Asking for it to not be in the global scope sounds like asking for special behaviour that deviates from the modern standard.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How is the global promise polyfill implemented in webpack v2?
Yes, this polyfill changes the global Promise . Calling it a polyfill usually means that it patches the global built-ins, although this is...
Read more >
babel/plugin-transform-runtime
A plugin that enables the re-use of Babel's injected helper code to save on ... such as Promise , Set and Map ,...
Read more >
3. Modularizing and Managing JavaScript - O'Reilly
Script loaders were created to provide a way of loading JavaScript files asynchronously, which means the rest of the page can continue loading...
Read more >
Upgrading from AngularJS to Angular
In both cases, the module loader will then take care of loading all the code ... and outputs should be bound to the...
Read more >
Node.js v16.7.0 documentation - Thomas Hunter II
An example of a web server written with Node.js which responds with 'Hello, World!' : Commands in this document start with $ or...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found