https://github.com/caub/acquire
async require
https://github.com/caub/acquire
Last synced: 3 months ago
JSON representation
async require
- Host: GitHub
- URL: https://github.com/caub/acquire
- Owner: caub
- Created: 2016-10-07T10:52:40.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-02-26T15:45:15.000Z (over 9 years ago)
- Last Synced: 2025-01-28T02:11:12.211Z (over 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 4.88 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## Async require
The syntax is like a classic CommonJS require, but it's asynchronous in the browser, so it works by adding yield (or await). Because of this, in this simple first version, it won't work when a require call is nested in a function.
file a.js:
```js
exports.PI = Math.PI;
exports.rand = n => Math.floor(10*Math.random());
```
file b.js:
```js
const {PI} = require('./a');
module.exports = deg => deg/180*PI;
```
file c.js:
```js
const [{rand}, toRadian] = require('./a', './b');
console.log('random angle:', toRadian(rand(360)))
```
could work on node with
```js
const {parse: URL} = require('url');
const {readFile} = require('fs'), fetch=url=>new Promise((res,rej)=>readFile(url, (err,data)=>err?rej(err):res({ text(){ return data }})));
// window, location, ..
```
### Tests
- https://caub.github.io/acquire/test.html
- https://caub.github.io/acquire/demo
- https://caub.github.io/todo-list/
inspired by [1](https://gist.github.com/caub/cf82c451120373dc1568#file-main-js)