@homer0/deferred
Advanced tools
Comparing version 3.0.0 to 3.0.1
@@ -16,2 +16,2 @@ type DeferredPromiseResolveFn<Value> = (value: Value) => void; | ||
export { DeferredPromise, DeferredPromiseRejectFn, DeferredPromiseResolveFn, deferred }; | ||
export { type DeferredPromise, type DeferredPromiseRejectFn, type DeferredPromiseResolveFn, deferred }; |
{ | ||
"name": "@homer0/deferred", | ||
"description": "Small utility to create a deferred promise", | ||
"version": "3.0.0", | ||
"version": "3.0.1", | ||
"repository": { | ||
@@ -26,8 +26,8 @@ "type": "git", | ||
"devDependencies": { | ||
"@homer0/eslint-plugin": "12.0.0", | ||
"@homer0/tsconfig": "1.0.12", | ||
"@homer0/eslint-plugin": "12.0.1", | ||
"@homer0/tsconfig": "1.0.13", | ||
"jest": "^29.7.0", | ||
"ts-jest": "^29.1.1", | ||
"tsup": "^7.2.0", | ||
"typescript": "^5.2.2" | ||
"tsup": "^8.0.1", | ||
"typescript": "^5.3.3" | ||
}, | ||
@@ -48,3 +48,3 @@ "engine-strict": true, | ||
}, | ||
"gitHead": "968248535be40f5ddb1cd53cdb2bb47c1c9a06d7" | ||
"gitHead": "3e1bd862a217e2d26a783144b991436e8b589260" | ||
} |
@@ -12,3 +12,3 @@ # 🕗 Deferred | ||
Using it is really simple, you call the `deferred()` function and get an object with `promise`, `resolve` and `reject`. You return the `promise` property and then call `resolve` or `reject` to either resolve the promise or reject it. | ||
Using it is really simple, you call the `deferred()` function and get an object with `promise`, `resolve` and `reject`. You return the `promise` property and, eventually, call `resolve` or `reject` to either resolve the promise or reject it. | ||
@@ -25,2 +25,6 @@ Now, a reason to use this is for when you have another service/function/something asking for something that your code hasn't even started to do: | ||
constructor() { | ||
this.defer = deferred(); | ||
} | ||
getFileContents(): Promise<string> { | ||
@@ -31,3 +35,3 @@ if (this.file) return Promise.resolve(this.file); | ||
async loadTheFile() { | ||
async loadTheFile(): Promise<void> { | ||
try { | ||
@@ -51,7 +55,7 @@ this.file = await fs.readFile('some-path', 'utf-8'); | ||
Ok, there's a lot going on this example, let's break it: | ||
Ok, there's a lot going on this example, so let's break it: | ||
1. `MyServiceThatLoadsAFile` creates a deferred promise on "its constructor". | ||
1. `MyServiceThatLoadsAFile` creates a deferred promise on its constructor. | ||
2. `getFileContents` should return the file contents, but because the file is not loaded yet (as `loadTheFile` has not been called), it returns the deferred promise. | ||
3. Eventually, `loadTheFile` gets called, it loads the file and either resolves or rejects the deferred promise, so the `getFileContents().then(...)` gets finally called. | ||
3. Eventually, `loadTheFile` gets called, it loads the file and either resolves or rejects the deferred promise, so the `getFileContents().then(...)` (or `catch` :P) gets finally called. | ||
@@ -62,9 +66,11 @@ I wanted to keep the example small, but on a real app, `getFileContents` is probably called by other service that has no idea the instance was just created or that `loadTheFile` hasn't been called yet. | ||
As this project is part of the `packages` monorepo, some of the tooling, like ESLint and Husky, are installed on the root's `package.json`. | ||
As this project is part of the `packages` monorepo, some of the tooling, like `lint-staged` and `husky`, are installed on the root's `package.json`. | ||
#### Tasks | ||
| Task | Description | | ||
| ------- | -------------------- | | ||
| `test` | Runs the unit tests. | | ||
| `build` | Bundles the project. | | ||
| Task | Description | | ||
| ------------- | ----------------------------------- | | ||
| `lint` | Lints the package. | | ||
| `test` | Runs the unit tests. | | ||
| `build` | Transpiles and bundles the project. | | ||
| `types:check` | Validates the TypeScript types. | |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11972
72