TGStat
TGStat
Type to search
Advanced channel search
  • flag English
    Site language
    flag Russian flag English flag Uzbek
  • Sign In
  • Catalog
    Channels and groups catalog Regional compilations Thematic compilations Платные каналы Search for channels
    Add a channel/group
  • Ratings
    Rating of channels Rating of groups Posts rating
    Ratings of brands and people
  • Analytics
  • Search by posts
  • Telegram monitoring
  • Promotion
    Advertising through Yandex Business Advertising in channels through TGStat Agency Advertising on TGStat.ru website
JavaScript Ready | Программирование

20 Aug, 14:12

Open in Telegram Share Report

Унифицируйте sync и async ошибки в один поток!

Частая проблема: функция может упасть либо сразу, либо внутри Promise.
function parse(data) {
return JSON.parse(data); // sync ошибка
}

Обычно приходится отдельно думать про оба случая.

Promise.try() превращает любой вызов в Promise:
const result = await Promise.try(() => {
return JSON.parse(input);
});

Теперь одинаково работают и обычные ошибки, и async-код:
await Promise.try(async () => {
const data = await fetch('/api');
return data.json();
});

🔥 Удобно для обёрток над чужими функциями, плагинов и API, где заранее неизвестно — вернётся значение или Promise.

📣 JS Ready | #совет

752 0 11 19
Catalog
Channels and groups catalog Channels compilations Search for channels Add a channel/group
Ratings
Rating of Telegram channels Rating of Telegram groups Posts rating Ratings of brands and people
API
API statistics Search API of posts API Callback
Our channels
@TGStat @TGStat_Chat @telepulse @TGStatAPI
Read
Академия TGStat Telegram Research 2019 Telegram Research 2021 Telegram Research 2023
Contacts
Справочный центр Support Email Jobs
Miscellaneous
Terms and conditions Privacy policy Public offer
Our bots
@TGStat_Bot @SearcheeBot @TGAlertsBot @tg_analytics_bot @TGStatChatBot