I have a library that needs to be called synchronously but has an async initialization step.
— Nicolò Ribaudo 🏳️🌈 (@NicoloRibaudo) September 12, 2020
Can I use pkg.exports or something else to use top-level await when supported, and fallback to export the initialization promise so that Node.js 12-13 users can await it themselves?
// package.json
{
"name": "your-pkg",
"exports": {
".": "./with-top-level-await.js",
"./init": "./without-top-level-await.js"
}
}
// >= node 14 (with top-level await support)
import api from 'your-pkg';
// < node 14
import init from 'your-pkg/init';
(async function() {
const api = await init();
})();