question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error between NATS and Promises

See original GitHub issue

Promise 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:closed
  • Created 3 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
jefer94commented, Sep 16, 2020

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

0reactions
aricartcommented, Sep 17, 2020

Nats.ts have that issue

(node:1) UnhandledPromiseRejectionWarning: TypeError: nc.on is not a function
    at /usr/src/services/current/node_modules/ts-nats/lib/nats.js:51:16
    at new Promise (<anonymous>)
    at Function.connect (/usr/src/services/current/node_modules/ts-nats/lib/nats.js:48:16)
    at Object.connect (/usr/src/services/current/node_modules/ts-nats/lib/nats.js:255:19)
    at Object.<anonymous> (/usr/src/services/current/dist/nats.js:56:21)
    at Module._compile (internal/modules/cjs/loader.js:1076:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1097:10)
    at Module.load (internal/modules/cjs/loader.js:941:32)
    at Function.Module._load (internal/modules/cjs/loader.js:782:14)
    at Module.require (internal/modules/cjs/loader.js:965:19)

Not seeing your code there, but connect on nats.ts returns a promise to the connection.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found