All Projects → xobotyi → Await Of

xobotyi / Await Of

Licence: mit
await wrapper for easier errors handling without try-catch

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Await Of

of
🍬 Promise wrapper with sugar 🍬
Stars: ✭ 13 (-94.58%)
Mutual labels:  wrapper, promise, error-handling, await, async-await
Await Handler
Basic wrapper for await that allows handling of errors without try/catch blocks
Stars: ✭ 13 (-94.58%)
Mutual labels:  promise, error-handling, async-await, await
P Map
Map over promises concurrently
Stars: ✭ 639 (+166.25%)
Mutual labels:  async, promise, async-await, await
P Iteration
Utilities that make array iteration easy when using async/await or Promises
Stars: ✭ 337 (+40.42%)
Mutual labels:  async, promise, async-await, await
Awaitkit
The ES8 Async/Await control flow for Swift
Stars: ✭ 709 (+195.42%)
Mutual labels:  async, promise, await
Swiftcoroutine
Swift coroutines for iOS, macOS and Linux.
Stars: ✭ 690 (+187.5%)
Mutual labels:  async, async-await, await
Wx Promise Pro
✨强大、优雅的微信小程序异步库🚀
Stars: ✭ 762 (+217.5%)
Mutual labels:  async, promise, await
Ws Promise Client
PROJECT MOVED: https://github.com/kdex/ws-promise
Stars: ✭ 6 (-97.5%)
Mutual labels:  async, promise, await
Then
🎬 Tame async code with battle-tested promises
Stars: ✭ 908 (+278.33%)
Mutual labels:  async, promise, async-await
Use Async Effect
🏃 Asynchronous side effects, without the nonsense
Stars: ✭ 193 (-19.58%)
Mutual labels:  async, async-await, await
Ws Wrapper
Lightweight WebSocket lib with socket.io-like event handling, requests, and channels
Stars: ✭ 58 (-75.83%)
Mutual labels:  wrapper, promise, browser
Emacs Async Await
Async/Await for Emacs
Stars: ✭ 47 (-80.42%)
Mutual labels:  async, promise, await
Rubico
[a]synchronous functional programming
Stars: ✭ 133 (-44.58%)
Mutual labels:  async, promise, async-await
Posterus
Composable async primitives with cancelation, control over scheduling, and coroutines. Superior replacement for JS Promises.
Stars: ✭ 536 (+123.33%)
Mutual labels:  async, promise, async-await
Kitsu
🦊 A simple, lightweight & framework agnostic JSON:API client
Stars: ✭ 166 (-30.83%)
Mutual labels:  async, promise, async-await
Promise Fun
Promise packages, patterns, chat, and tutorials
Stars: ✭ 3,779 (+1474.58%)
Mutual labels:  async, promise, async-await
Asyncex
A helper library for async/await.
Stars: ✭ 2,794 (+1064.17%)
Mutual labels:  async, async-await, await
retryx
Promise-based retry workflow library.
Stars: ✭ 21 (-91.25%)
Mutual labels:  promise, error-handling, async-await
Evt
💧EventEmitter's typesafe replacement
Stars: ✭ 305 (+27.08%)
Mutual labels:  promise, async-await, browser
Breeze
Javascript async flow control manager
Stars: ✭ 38 (-84.17%)
Mutual labels:  async, promise, await

❤️Please consider starring this project to show your love and support.🙌

About

ES7 async/await gives to developers ability to write asynchronous code that look like synchronous. But under the hood it is still just a sugar on top of the ES6 Promise.
You can write code that looks clean, but only unless you have to catch errors. To catch thrown error or handle the promise's rejection you have to surround it with try-catch block or fallback to pure promises and from that moment visual purity of your code is over.
But there is a solution!☀️
I really like the way it's done in Go. It has no error throwing mechanism, but has a multi-value return and the common way to handle errors in Go is to return error as a last value, like so:

data, err := someErrorFunc(someStuff)
if err != nil {
    return err
}

But JavaScript has no multi-value return! - you would say. Sad, but true.
But!
It has a destructuring assignment and await-of gives you ability to do this:

import { of } from "await-of";

async () => {
  let [res, err] = await of(axios.get("some.uri/to/get"));

  if (err) {
    // rethrow if its not an axios response error
    if (!err.response) {
      throw err;
    }

    res = err.response;
  }

  const { data, status = 0 } = res;

  console.log(data, status);
};

There is no modifications needed in function/promise you want to await - just pass it to the of() and whole the magic will be done.

Installation

npm i --save await-of

Usage

import { of } from "await-of";

async function someAsyncStuff() {
  let error, data;

  // if we don't want to handle error
  [data] = await of(Promise.reject(new Error("ERROR!")));
  console.log(data); // undefined

  // if promise was rejected - it's rejection value will be treated as error
  [, error] = await of(Promise.reject(new Error("ERROR!")));
  console.log(error); // ERROR!

  // or if promise has any uncaught errors it'll catch them too!
  [, error] = await of(
    new Promise(() => {
      throw new TypeError("ERROR!");
    })
  );
  console.log(error.message); // ERROR!
}

Tests

# install dependencies if you haven't yet
npm install
npm run test
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].