Async done simple
Tags: javascript
Published: 2017-09-17 19:23:59

---
title: Async done simple
published: true
description: 
tags: javascript
---

Javascript is single core programing language. That can be good thing if you want to interact with HTML DOM(document object model), or you are doing small project. In other cases if you are working on mean stack(MongoDB, express, angularjs, node.js) or other system you are probably using async functionality.

How does it works:

Best way to understand is from example. Let's say that you are in a coffee shop and you want one cup of black coffee. You order you coffee from Maria, she call's you when is ready, and you get your coffee. Your desire for coffee is new async call, When Maria say wait for coffee that is promise. Maria calls you that your order is ready is resolve. if there are any problems Maria will tell you and that is reject.

If you are using "vanilla" javascript async without any packages it looks something like this:

    function myAsyncFunction(url) {
        return new Promise((resolve, reject) => {
            Your code goes here
        });
    };

It looks simple, we sould all do it like that! 

NO!

Here is why:

For me personally, putting all code inside another function is bad for reading the code, it can get messy, clutted and hard to read. If I can, I will never put my code inside of another function(exept for callback functions).

Its good for small functions, that dont have to many line, and they are simple.

Then I thought to myself how I can improve it, make it object oriented and simple. After week and a half I came with this solution:


    exports.simpleAsync = function () {
        this.resolve;
        this.reject;
        this.promise = new Promise((resolve, reject) => {
	        this.resolve = resolve;
	        this.reject = reject;
        });
    };

It is simple, clean, lightweight and easy to use. It can be used similarly like good old q pacakge for async.

How should you use it:

    var Async = require('simplify-async').simpleAsync;

        function test() {
            var promise = new Async();
	
            setTimeout(() => {
	        promise.resolve("Have nice day");
            }, 5000);

            return promise.promise;
        }

    test().then((response) => {
        console.log(response)	
    }).catch((err) => {
	    console.log(err);
    });

Let me expalin what is goind on:

Test is async function. First thing is that you have to make new instance of Async package that you imported with require function. Next is that you have to return promise, because if you don't js will return error that goes something like this: cannot read property .then of undefined. After you get your data, all you need to do is call promise.resolve funtion and async call will be finished.

I hope this small, and super simple async package will help you do more!

If you want to checkout my package it's on github:

https://github.com/PetarProkopenko/simple-async

Link to npm package is:

https://www.npmjs.com/package/simplify-async

Note: I'm not doing this just for promotion, I'm doing it becuse I love programming and I want to contribute to community.
This is my first article and package, if you have any idea let me know, or if I did something wrong...
Thank you for your time and for reading, I hope you have nice day.