Skip to content

A wrapper on Promises. Make you code more readable and async looks like sync

License

Notifications You must be signed in to change notification settings

aeciolevy/to-await

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

To-await

To-await is an efficient and clean way to write Asynchronous code as Synchronous code.

It it based on this article - How to write async await without try-catch blocks in Javascript it also has the only addition that we support Promise.All.

I have been using this technique for more than a year in production. It is a pretty simple approach to avoid callback hell and promise chains. Furthermore, I found my self nesting try and catch. These are the primary cases where this package alleviates issues.

Installation

npm install to-await

This assume that you are using npm as package manager.

The Gist

ES6 modules

import { to, toAll, toAllSettled } from 'to-await';

CommonJS

const { to, toAll } = require('to-await');

Promise Example

import { to, toAll } from 'to-await'

const firstPassTest = async () => {
    try {
        const return10 = () => Promise.resolve(10);
        const rejected = () => Promise.reject(new Error("Something wrong"));

        let [error, result] = await to(return10());
        console.log(`error: ${error}, result: ${result}`);

        [error, result] = await to(delay(3000));
        console.log(`error: ${error}, result: ${result}`);
        [error] = await to(rejected());
        console.log(`error2: ${error}`);
        if (error) {
            throw error;
        }
    } catch (err) {
        console.error(err);
    }
};

firstPassTest();

Array of Promisses

const { to, toAll } = require('to-await');
const secondTest = async () => {

    const arrayOfPromises = array => array.map(async (el, index) => 1 + index);
    const promises = arrayOfPromises([1, 2, 3, 4, 5, 6, 7]);
    let [error, data] = await toAll(promises);
    // Version 1.1.0 accept an array on to function
    // toAll with be left for backward compatibility
    // you can deal with the array of promises directly to the to function
    let [error, data] = await to(promises);
    console.log(`error: ${error}, data: ${data}`);
}

secondTest();

Promise.allSettled Support

const { to } from 'to-await'

const promises = [Promise.resolve(1), Promise.resolve(2), Promise.reject(new Error('Something went wrong'))];
let [rejected, fulfilled] = await toAllSettled(promises);

Console.log Output


     rejected = [{
        status: 'rejected',
        reason: Error: Something went wrong
            at Object.<anonymous> (/Users/aeciolevy/npm-packages/to-await/test/index.test.ts:82:78)
            at Object.asyncJestTest (/Users/aeciolevy/npm-packages/to-await/node_modules/jest-jasmine2/build/jasmineAsyncInstall.js:100:37)
            at /Users/aeciolevy/npm-packages/to-await/node_modules/jest-jasmine2/build/queueRunner.js:43:12
            at new Promise (<anonymous>)
            at mapper (/Users/aeciolevy/npm-packages/to-await/node_modules/jest-jasmine2/build/queueRunner.js:26:19)
            at /Users/aeciolevy/npm-packages/to-await/node_modules/jest-jasmine2/build/queueRunner.js:73:41
            at processTicksAndRejections (internal/process/task_queues.js:93:5)
      }
    ]
    fulfilled = [
      { status: 'fulfilled', value: 1 },
      { status: 'fulfilled', value: 2 }
    ]

toAllSettled interface

import { toAllSettled } from 'to-await'
let mixedPromises = [Promise.resolve(1), Promise.reject(new Error('something went wrong')), Promise.resolve(2)];
let [rejected, fulfilled] = await toAllSettled(mixedPromises);

Options

{
    // if object, return an object with { error, data }
    // or to toAllSettled { rejected, fulfilled }
    parser: 'array'(default) | 'object'
}

You can destructure a object (options object)

// to function
const return10 = () => Promise.resolve(10);
let {error, data} = await to(return10(), { parser: 'object'});
// toAll function
const promises = [Promise.resolve(1), Promise.resolve(2)];
let { error, data } = await toAll(promises, { parser: 'object' });
// toAllSettled function
const promises = [Promise.resolve(1), Promise.resolve(2), Promise.reject(new Error('something wrong'))];
let { rejected, fulfilled } = await toAll(promises, { parser: 'object' });

Where it might help you

In this case below, if you want to use a try catch to wrap you function, you are not able to throw an error inside of the callback/Promise since you are inside of another scope. This is an example from nodejs docs.

const fs = require('fs');
const { promifisy } = require('util');

const writeAsync = promisify(fs.writeFile);
const data = new Uint8Array(Buffer.from('Hello Node.js'));

function foo() {
    try {
        fs.writeFile('message.txt', data, (err) => {
            // this throw will not be catch by the throw
            // because you are inside the callback
            if (err) throw err;
            console.log('The file has been saved!');
        });
    } catch (err) {
        console.error(err);
    }
}

async function fooNicer() {
    try {
        const [error] = await to(writeAsync('message.txt', data, ));
        if (error) {
            throw error;
        }
        console.log('The file has been saved!');
    } catch (err)
}

About

A wrapper on Promises. Make you code more readable and async looks like sync

Resources

License

Stars

Watchers

Forks

Packages

No packages published