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

Promise based module loading #1769

Open
unit-404 opened this issue Feb 8, 2019 · 7 comments
Open

Promise based module loading #1769

unit-404 opened this issue Feb 8, 2019 · 7 comments

Comments

@unit-404
Copy link

unit-404 commented Feb 8, 2019

If requirejs used for Web, so I suggest to add new syntax, based on await/async. Also, I suggest make script loading and caching, based on FileSystem API and file system URL.

(async ()=>{
  let module = await require("module.js"); // just require module with await syntax
})();

Or

// here "async" wrapper omitted 
let modules = require(["a.js", "b.js", "c.js"]); // load tarball of modules
let a = await modules[0]; // await this module (for example, caching or loading from cache)
@ddziaduch
Copy link

Nice idea, but... Many browser supports now ES6 module by default https://caniuse.com/#feat=es6-module and that support will only increase :)

@chrisknoll
Copy link

Not to start a flame war here, but, that ES6 module support (via script type=module) is no where close to a fully formed module loading system. They still need to work out loading semantics and name resolution before any of this goes anywhere (basically all the framework stuff you get with a loader like RequireJS).

@ddziaduch
Copy link

@chrisknoll do you have any article where I can read about it? It sounds interesting

@chrisknoll
Copy link

There's a few resources on the web that will give you the history, but the main source of info comes out of the WhatWG working group that is developing the standard: https://github.com/whatwg/loader.

Here's a fun nugget from their main README:

Status

A good chunk of this spec is out of date and is undergoing revision to realign it with Service Worker, the JS Realms API, and the dynamic import() feature.

Yikes. They've only been working on it for about 8 years now, and the're back at the drawing board.

If you look at how the node community is handling it, they decided to implement an extension .mjs aka 'The Michael Jackson Script' to differentiate a CJS from an ES6 module. That's what it's come to...defining new MIME types.

So, it's a mess, and from the node community standpoint, they have so much use of their code system, they can basically call the shots which leaves the asynchronous folks who work exclusively out of the web to rely on transpilers and build chains. Of course any node developer will tell you that build chains is a fact of life, but whenever I observe a node dev doing server side programming, there's no build step there! only for the browser....hmm

Anyways, do a couple of searches, you'll find how far ES6 is from being a complete solution.

@AllNamesRTaken
Copy link

So, is there a techincal reason that we cannot return a Promise and just resolve as when we would call the callback? I must be missing something but it feels straight forward?

If we only need someone to do it then that should be easy enough but I guess there are other things blocking.

@ddziaduch
Copy link

@AllNamesRTaken it would require a Polyfil because older browsers does not support Promise

@kohlmannj
Copy link

kohlmannj commented Oct 19, 2019

For what it’s worth, you can wrap a call to RequireJS’s require() function in a new Promise like this:

async function main() {
  const [dep1, dep2, dep3] = await new Promise((resolve, reject) => {
    require(['dep1', 'dep2', 'dep3'], resolve, reject);
  });
}

main();

Compared to awaiting multiple dynamic import() calls, I found this to be close in terms of ergonomics. Also, it works!

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

5 participants