New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

async-wrap

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

async-wrap

Library with wrapper functions for async function (or functions, that return Promises)

  • 1.0.5
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
5
increased by25%
Maintainers
1
Weekly downloads
 
Created
Source

async-wrap

Library with wrapper functions for async function (or functions, that return Promises)

Install

npm install async-wrap

Usage

var asw = require('async-wrap')

asw.coalesce([1,2,3],asyncfun)

or

var coalesce = require('async-wrap/coalesce')

coalesce([1,2,3],asyncfun)

Overview

  • coalesce

Functions

coalesce

Accepts two parameters:

  • elements {Array}: List of elements that should be passed to the callback
  • callback {function}: Async callback function (promise) that accepts an element as parameter

Use coalesce to execute a async function again, when it fails, with the next element in the elements list. E.g. you have multiple instances of a service, and want to retry the request with the next service instance if you can't connect to the first service.

Example:

Interactive example on runkit

function getItems(){
    var urls = getServiceUrls() // Returns array
    return coalesce(urls, requestItems)

    async function requestItems(url) {
        return asyncRequest(url+'/api/items')
    }
}

Same using Promise:

function getItems(){
    var urls = getServiceUrls() // Returns array
    return coalesce(urls, requestItems)

    function requestItems(url) {
        return new Promise((resolve, reject) => {
            request(url+'/api/items',(error, body) => {
                if (error) reject(error)
                else resolve(body)
            })
        })

    }
}

With multiple parameters:

function getItemInfo(id){
    var urls = getServiceUrls() // Returns array
    return coalesce(urls, (url) => requestItemInfo(url,id))

    async function requestItemInfo(url, id) {
        return asyncRequest(url+'/api/item/'+id)
    }
}

Keywords

FAQs

Package last updated on 10 Nov 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc