An open API service indexing awesome lists of open source software.

https://github.com/caub/acquire

async require
https://github.com/caub/acquire

Last synced: 3 months ago
JSON representation

async require

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)