1.1.1 • Published 1 year ago

better-async v1.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

better-async

Useful package to work with promises.

BetterPromise class

This class has 2 utility method:

  • objectAll
  • objectAllSettled

BetterPromise.objectAll()

Same usage of Promise.all method (MDN Documentation), but it accept a Record<string, Promise<T>> in input and return a Record<string, T>.

Usage example:

  • case with standard Promise.all
const responses = await Promise.all([
    externalApiRequest1(),
    externalApiRequest2(),
    timeConsumingProcess(),
]);

console.log(responses[0]); // result of external api request 1
console.log(responses[1]); // result of external api request 2
console.log(responses[2]); // result of time consuming process
  • case with BetterPromise.objectAll
const responses = await BetterPromise.objectAll({
    extApi1: externalApiRequest1(),
    extApi2: externalApiRequest2(),
    process: timeConsumingProcess(),
});

//as you can see, the code is more clear in this way. You don't need to remember which thing are in which position
console.log(responses.extApi1); // result of external api request 1
console.log(responses.extApi2); // result of external api request 2
console.log(responses.process); // result of time consuming process

BetterPromise.objectAllSettled()

Same usage of Promise.allSettled method (MDN Documentation), but it accept a Record<string, Promise<T>> in input and return a Record<string, PromiseSettledResult<T>>.

Usage example:

  • case with standard Promise.allSettled
const responses = await Promise.allSettled([
    externalApiRequest1(),
    externalApiRequest2(),
    timeConsumingProcess(),
]);

console.log(responses[0].status); // status of external api request 1
console.log(responses[1].status); // status of external api request 2
console.log(responses[2].status); // status of time consuming process
  • case with BetterPromise.objectAllSettled
const responses = await BetterPromise.objectAllSettled({
    extApi1: externalApiRequest1(),
    extApi2: externalApiRequest2(),
    process: timeConsumingProcess(),
});

//as you can see, the code is more clear in this way. You don't need to remember which thing are in which position
console.log(responses.extApi1.status); // status of external api request 1
console.log(responses.extApi2.status); // status of external api request 2
console.log(responses.process.status); // status of time consuming process
1.1.1

1 year ago

1.1.0

1 year ago

1.0.0

1 year ago