- 将所有图片路径从绝对路径改为使用 process.env.PUBLIC_URL - 修复 HomePage.tsx 中所有图片引用 - 修复 CoursePage.tsx 中所有图片引用 - 确保图片在 GitHub Pages 上正确加载 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
77 lines
2.0 KiB
Markdown
77 lines
2.0 KiB
Markdown
# universalify
|
|
|
|

|
|

|
|

|
|

|
|
|
|
Make a callback- or promise-based function support both promises and callbacks.
|
|
|
|
Uses the native promise implementation.
|
|
|
|
## Installation
|
|
|
|
```bash
|
|
npm install universalify
|
|
```
|
|
|
|
## API
|
|
|
|
### `universalify.fromCallback(fn)`
|
|
|
|
Takes a callback-based function to universalify, and returns the universalified function.
|
|
|
|
Function must take a callback as the last parameter that will be called with the signature `(error, result)`. `universalify` does not support calling the callback with three or more arguments, and does not ensure that the callback is only called once.
|
|
|
|
```js
|
|
function callbackFn (n, cb) {
|
|
setTimeout(() => cb(null, n), 15)
|
|
}
|
|
|
|
const fn = universalify.fromCallback(callbackFn)
|
|
|
|
// Works with Promises:
|
|
fn('Hello World!')
|
|
.then(result => console.log(result)) // -> Hello World!
|
|
.catch(error => console.error(error))
|
|
|
|
// Works with Callbacks:
|
|
fn('Hi!', (error, result) => {
|
|
if (error) return console.error(error)
|
|
console.log(result)
|
|
// -> Hi!
|
|
})
|
|
```
|
|
|
|
### `universalify.fromPromise(fn)`
|
|
|
|
Takes a promise-based function to universalify, and returns the universalified function.
|
|
|
|
Function must return a valid JS promise. `universalify` does not ensure that a valid promise is returned.
|
|
|
|
```js
|
|
function promiseFn (n) {
|
|
return new Promise(resolve => {
|
|
setTimeout(() => resolve(n), 15)
|
|
})
|
|
}
|
|
|
|
const fn = universalify.fromPromise(promiseFn)
|
|
|
|
// Works with Promises:
|
|
fn('Hello World!')
|
|
.then(result => console.log(result)) // -> Hello World!
|
|
.catch(error => console.error(error))
|
|
|
|
// Works with Callbacks:
|
|
fn('Hi!', (error, result) => {
|
|
if (error) return console.error(error)
|
|
console.log(result)
|
|
// -> Hi!
|
|
})
|
|
```
|
|
|
|
## License
|
|
|
|
MIT
|