Error between NATS and Promises
See original GitHub issuePromise is resolved with success status
export function subscribe<T>(service: string): Promise<Record<string, unknown>> {
return new Promise((resolve, reject) => {
resolve({ token: '!aasdasdasdsadsa' }) // in this scope if I call resolve, I get resolve this promise
nc.subscribe(service, function (msg): void {
} )
})
}
Promise is never resolved with success status
export function subscribe<T>(service: string): Promise<Record<string, unknown>> {
return new Promise((resolve, reject) => {
nc.subscribe(service, function (msg): void {
resolve({ token: '!aasdasdasdsadsa' }) // in this scope if I call resolve, nothing happen
} )
})
}
Ticket #182 that not work to me
Example of my use case
// ./nats.ts
type Msg = Record<string, unknown>
type FetchMsg = (_: unknown, message: Msg, ctx: Request) => Promise<Msg>
// GraphQL resolver
export function fetchMsgWithAuth(service: string, project: string): FetchMsg {
return (_, message, ctx) => {
if (ctx.auth?.user) {
const channel = publish(service, project, message)
return subscribe(channel)
}
throw ctx.error
}
}
// that function is my issue
export function subscribe<T>(service: string): Promise<T> {
return new Promise((resolve, reject) => {
const id = nc.subscribe(service, (msg) => {
if (msg.error) reject(msg.error)
else resolve(msg.data)
nc.unsubscribe(id)
})
})
}
export function publish<T>(service: string, action: string, message: Record<string, T>): string {
const channel = keychain(service) // @chocolab/keychain, provide incremental string, this maybe unique just if that service contain only one instance
nc.publish(service, { type: action, ...message }, channel)
return channel
}
// ./resolvers/query.ts
export enum projectsRefs {
...
getProjectPermission = 'get project permission',
...
}
export enum serviceRefs {
...
projects = 'projects',
}
const projects = {
...
getProjectPermission: fetchMsgWithAuth(serviceRefs.projects, projectsRefs.getProjectPermission)
}
export default { ...authenticator, ...projects }
I just want publish one request, get response and close the subscription, thanks for you time
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
Promises · Issue #24 · nats-io/nats.js - GitHub
So in node, the pattern of callbacks is function(error, result) where the first argument is always the error and the second argument is...
Read more >Asynchronous Subscriptions - NATS Docs
Asynchronous subscriptions use callbacks of some form to notify an application when a message arrives. These subscriptions are usually easier to work with, ......
Read more >nats - npm
Node.js client for NATS, a lightweight, high-performance cloud native messaging system. Latest version: 2.9.0, last published: 6 days ago.
Read more >nats/index.d.ts - UNPKG
The CDN for nats. ... 11, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ... 39, closed(): Promise<void |...
Read more >Subscription | ts-nats
Documentation for ts-nats. ... Returns Promise<any> ... the subscription's callback with an error argument if the expected number of messages (specified via ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Ok, nats.ts will merge with nats.js in some future release? It would be great if in the future it is just a repo
Not seeing your code there, but
connect
on nats.ts returns a promise to the connection.