await-to-js
await-to-js copied to clipboard
Add named export for more convenient usage for applications using ES modules
Problem
Currently example usage (written for CommonJS modules) doesn't work for ES modules (aka ESM where application has "type": "module"
in package.json
), because default exports don't play nice with ESM.
This affects both TypeScript and vanilla JS (with ES, not CommonJS modules), but if you'd use TypeScript, you would get a compile-time error when using like in the example from readme:
import to from 'await-to-js'
...
const [err, data] = await to<ServerResponse>(p)
src/main.ts:9:27 - error TS2349: This expression is not callable.
Type 'typeof import("/home/ats/proj/easypark/productivity/github-patcher/node_modules/await-to-js/dist/types/await-to-js")' has no call signatures.
9 const [err, data] = await to<ServerResponse>(p)
~~
Workaround
To work around it, you would need to replace to(
with to.default(
, like this:
const [err, data] = await to.default<ServerResponse>(p)
This is nasty - it requires different usage for ESM and CommonJS (can't use the same approach for both module systems).
ESM compatible nicer solution
Instead of exporting only default export (that would keep backwards compatibility for CommonJS), you could also add named export for the same function, so that applications, that use ES modules could use
import { to } from 'await-to-js'
const [err, data] = await to(p)
(that also would work for CommonJS modules) instead of the default import + workaround mentioned above:
import to from 'await-to-js'
const [err, data] = await to.default(p)
这是来自QQ邮箱的假期自动回复邮件。你好,我最近正在休假中,无法亲自回复你的邮件。我将在假期结束后,尽快给你回复。