1.0.1 • Published 3 years ago

js-async-utils v1.0.1

Weekly downloads
-
License
MIT
Repository
-
Last release
3 years ago

js-async-utils

Utilities for asynchronous tasks and functions

promiseWrapper

To run asynchronous tasks, we use promises. And in some use cases, we use it often in our project. Hence I have implemented promiseWrapper to simplify the syntax.

Eg. Standard way

const promise = (args) =>{
  return new Promise(async(resolve, reject)=>{
     try{
      //exectue task

      //To reject
      if(condition){
        reject('message')
      }

      resolve(data)
     }catch(err){
      reject(err)
     }
  }
}

//We can make this even simpler using promiseWrapper from async-utilities package

import { promiseWrapper } from 'js-async-utils';

const promise = promiseWrapper(async args => {
  //Execute task

  //To reject
  if (condition) {
    throw new Error('message');
  }

  return data;
});

//To run this eg.

const executePromise = async () => {
  try {
    const promiseResult = await promise(args);
  } catch (err) {
    //HandldeError
  }
};
1.0.1

3 years ago

1.0.0

3 years ago