What could it be? [TsLint Error: "Promises should be handled accordingly"]

I do some basic asynchronous operations using async/awaitTypeScript, but TSLint throws mysterious error messages for these two functions below. Has anyone encountered these errors before? The control rule is not mentioned in the error output, so I don’t understand what causes them. Any ideas would be greatly appreciated.

Basic request:

import * as rp from 'request-promise'

export function getRequest(address: rp.Options): rp.RequestPromise {
  return rp(address)
}

Exported async function:

export async function getStatus(message: Message) {
  try {
    const res = await getRequest(address)
    if (res.ready) {
      message.reply('...')
    } else {
      message.reply('...')
    }
  } catch (err) {
    message.reply(err)
  }
}

This gets: Promises must be handled appropriatelyand await of non-Promisefor line # 3.

A simple function using this export:

client.on('message', message => {
  if (message.content === 'green') {
    getStatus(message)
  }
})

It also gets Promises must be handled appropriately.

Additional Information:

, , Promises must be handled appropriately: https://palantir.imtqy.com/tslint/rules/no-floating-promises/

await of non-Promise: https://github.com/palantir/tslint/issues/2661

+23
5

. ,

Promise .catch .then ().

, ,

PromiseFunction()
  .catch(err => handle(err))
  .then(() => console.log('this will succeed'))

tslint, .then(...) - , . .catch, ,

PromiseFunction()
  .catch(err => handle(err))
  .then(() => console.log('this will succeed'))
  .catch(() => 'obligatory catch')

tslint :

PromiseFunction()
  .catch(err => handle(err))
  // tslint:disable-next-line:no-unsafe-any
  .then(() => console.log('this will succeed'))

.then .catch. , .then , , , , .

+46

, . - .

:

promiseFunction().then().catch()
try/catch async/await

:

void promiseFunction();
+10

getStatus :

// All functions marked as async returns a promise:
async function getStatus(message: Message) {/* ... */}

getStatus, :

getStatus(message)

, . , , .then():

getStatus(message).then(() => console.log('done'));
+4

, ​​ , getStatus, . , . , .

lint :

client.on('message', message => {
 if (message.content === 'green') {
   await getStatus(message)
}});

-, . , .

+1
source

I have the same exception when I created firebase-functionusingfirebase-tool

const ref = admin.database().ref("path/to/database/object");

ref.once("value").catch(error =>{  // line 22
    response.send( error() );
}).then( snapshot =>{
    response.send( snapshot.val );
})

This code is not compiled and return

ERROR: /src/index.ts[22, 5]: Promises must be handled appropriately

I changed places catchand then.

ref.once(...).then(...).catch(...)

This code works, sorry, but I have no explanation

It is so surprising that the application returns some error without a block, catcheven in accordance with firebase docwithout mentioning what is required catch.

+1
source

Source: https://habr.com/ru/post/1017268/


All Articles