async-is-fun

yarn add async-is-fun // or npm install --save async-is-fun

Usage no npm install needed!

<script type="module">
  import asyncIsFun from 'https://cdn.skypack.dev/async-is-fun';
</script>

README

Async is Fun

A collection of small, composable helper functions and utilities for doing fun stuff with promises.

Installation

Node

yarn add async-is-fun
// or
npm install --save async-is-fun

Webpack

Documentation

Table of Contents

async-is-fun

defer

An overlooked pattern for Promise usage, returns An old school self contained deferred object. A fun use of this is waiting for async instantiation in class constructors.

Type: object

Properties

  • resolve function (any) resolve the Promise with value.
  • reject function (any) reject the Promise with value.
  • promise Promise

Examples

const defer = require('async-is-fun').defer

 class AsyncConstructor {
   constructor(promise){
     this.Promise = promise.promise
     setTimeout(() => {
       promise.resolve(Math.random())
     }, 1000)
   }
 }

 let later = new AsyncConstructor(defer())
 let evenLater = new AsyncConstructor(defer())

 Promise.all([later.Promise, evenLater.Promise])
 .then((results)=>{
   console.log(results) // [ 0.17888717674897858, 0.8638054083464473 ]
 })

delay

Delays resolution of a Promise by [time] amount, resolving [value]

Yes, it is true that plenty of other libraries implement this method, but is included here because it is used internally and could save someone from having to load a second bigger library like bluebird.

Parameters

Examples

const delay = require('async-is-fun').delay

Promise.resolve('Wait for it')
.then(delay(1000))
.then((result)=>{
  console.log(result) // 'wait for it'
}

Returns function (any)

retryUntil

Retry a Promise returning function

Type: function

Parameters

  • fun function A promise or value returning function, values will be wrapped in a Promise.
  • config object? The optional configuration object.
    • config.timeout object Initial timeout value before retry. (optional, default 250)
    • config.bound object Upper bound of the computed timeout. (optional, default 1000)
    • config.growth object Growth rate of the timeout per iteration. src/growIt.js (optional, default 0.2)
    • config.tries object Number of attempts before the promise is rejected. (optional, default 10)
    • config.returnErrors object Number of errors to report if [func] throws or rejects. (optional, default 5)

Examples

const retryUntil = require('async-is-fun').retryUntil

Promise.resolve({msg: 'Retry a bunch', value: 10})
.then(retryUntil((value)=>{

  //Value is passed through.
  console.log(value) // {msg: 'Retry a bunch', value: 10}

  return somePromiseReturningFun(9)
  .then((count)=>{
    console.log(count) // 9
    let total = count + value

    //If a truthy value is returned the promise will resolve with that value.
    if(total === 15){
      return {total: total}
    }

    //If we return false, the method will retry after the timout expires.
    return false
  })
}))
.then((result)=>{
  console.log(result) // {total: 19}
}

Returns function (value) Returns a Promise returning function that will resolve the first truthy value returned or resolved by the provided function. It will reject when all attempts are exhausted.