GithubHelp home page GithubHelp logo

asde's Introduction

SYNOPSIS

A low level module for destructuring asynchronous values.

MOTIVATION

Use async/await on regular callback APIs, return [error||null, ...values].

DESCRIPTION

asde is a low level module that wraps a node-style function call in a promise so that it can be used with async/await. The promise always resolves with an array of values (error first). The values can then be identified using a destructuring assignment, providing both the error and values in the same channel without expectation of a try/catch block.

USAGE

Here are some notes on how async functions work with the await keyword.

ASYNC

async functions help solve promise hell. They provide a synchronous-looking way to receive asynchronous values.

async function main () {
  // the await keyword is only available inside async functions.

  // async functions are "shallow"! await is not available
  // to any nested, non-async functions in the scope.

  // an exception in an async function body
  // will throw an unhandled promise rejection.

  // try/catch blocks can be used to handle rejections.
}

AWAIT

The await keyword waits until a promise resolves or rejects.

const b = new Promise((resolve, reject) => {
  setTimeout(() => resolve('b'), 16)
})

async function main () {
  try {
    console.log('a')
    console.log(await b)
    console.log('c')
  } catch (ex) {
    console.log(ex)
  }
}

main()

If you don't catch, the rejection will "bubble up" until it finds a promise that can handle it. If one isn't found, Node.js (and some browsers) provide a catch-all event called unhandledRejection (similar to uncaughtException).

USAGE

asde will return a function that takes the same arguments as the original function, but does not require a callback. The new function should be called with the await keyword from within a async function.

const asde = require('asde')
const fs = require('fs')

const stat = asde(fs.stat)

async function main () {
  const [errStat, value] = await stat('./file')

  if (errStat) {
    return console.error(errStat)
  }

  console.log(value)
}

main()

INSTALL

npm install asde

asde's People

Contributors

heapwolf avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.