We want to solve the callback-hell that exists in calling to Async function in Node.JS

137 views
Skip to first unread message

ami

unread,
May 4, 2015, 11:12:13 AM5/4/15
to nod...@googlegroups.com
Hello everyone.

I thought about a solution how to solve the callback hell.

var a=asyncFunction1(parameters,function(err,param1){
  var b=asyncFunction2(parameters,function(err,param2){  
     if (err) console.log(err)
     console.log('here')
  }

I want to create new syntax for calling to async functions.
The new syntax will be as extension to JavaScript (like TypeScript, or CoffeScript but minimalist change to the JS code).
It will be compiled just before Node.jS running the JavaScript.

I have created new GitHub repository. And I want to start developing it. (https://github.com/AminaG/HeavenScript)
I am searching for other developers, who want to join me in this project.
I need help in 2 things:
1. IDE's intellisense. Because I am changing little bit the JavaScript, all IDE's will create warnings. So I need to create new definition files for them (IntelliJ, SublimeText, VS, etc;) Who here know how to do it?
2. Help me with English. My development skills, are much better than English. So for explanations, and create README file, I need help.

//******************************
the new syntax:

a=@asyncFunction1(param1)
err,b=@asyncFunction2(param2)
console.log('now we can access to a,b')

it will be converted to

asyncFunction1(param1,function(err,a){
  if(err) throw err; 
  asyncFunction2(param2,function(err,b){
    console.log('now we can access to param1, param2')
  })
})


//******************************
One More Example:

See how much it is easier and readable to write it in HeavenScript

function theBlock(){
  a=@asyncFunction1(param1)
  err,b=@asyncFunction2(param2)
  console.log('Now we can access a,b,err')
  if (err) {console.log('error in function2'),
  err,c=@asyncFunction3(param3) {
      console.log('Now we can access a,b,c')
  }
  d=@asyncFunction4(param4)
  console.log('Now we can access only to a,b. c is in another scope')
}

(304 characters)

It will be converted to:

asyncFunction1(param1,function(err,a){
  if(err) throw err
  asyncFunction2(param2,function(err,b){
    console.log('Now we can access a,b,err')
    if (err) console.log('error in function2')
      asyncFunction3(param3,function(err,c){
         console.log('Now we can access a,b,c')
      })
      d=asyncFunction4(param4,function(err,d){
          if(err) throw err
           console.log('Now we can access only to a,b. c is in another scope')
     })
    })
})


(391 characters)

What do you think?

John Wathen

unread,
May 4, 2015, 5:02:28 PM5/4/15
to nod...@googlegroups.com
Have you taken a look at https://www.npmjs.com/package/async? You still have a callback at the end but it can help to clean up a lot of the deep nesting.

async.series([a, b], function(err, results) {
  console.log(results); // an array holding the results of a and b
});

Bruno Jouhier

unread,
May 6, 2015, 9:49:46 AM5/6/15
to nod...@googlegroups.com
You should take a look at https://github.com/Sage/streamlinejs

Same basic idea but it goes beyond simple sequencing of async calls; it handles async calls in every JS construct.

Also, async/await is being considered for ES7 and there are already some polyfills that implement it with generators. See http://babeljs.io/docs/usage/transformers/other/async-to-generator/ for example.

Bruno

Branden Visser

unread,
May 6, 2015, 9:49:59 AM5/6/15
to nod...@googlegroups.com
Hi ami, if you haven't already, I might recommend having a look at the
Promises/A+ spec [1] and some implementations such as bluebird [2]
before deciding more language support is necessary to resolve
aesthetic callback issues.

Hope that helps,
Branden

[1] https://promisesaplus.com/
[2] https://github.com/petkaantonov/bluebird
> --
> Job board: http://jobs.nodejs.org/
> New group rules:
> https://gist.github.com/othiym23/9886289#file-moderation-policy-md
> Old group rules:
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> ---
> You received this message because you are subscribed to the Google Groups
> "nodejs" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to nodejs+un...@googlegroups.com.
> To post to this group, send email to nod...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/nodejs/a73509d3-9f00-42f2-a7dd-7c5875e86a65%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.

Alex Kocharin

unread,
May 6, 2015, 9:50:01 AM5/6/15
to nod...@googlegroups.com
 
Well that's what streamline.js does. I'd argue creating a new language just for the purpose of control flow management is a poor idea, but whatever.
 
With generators you can already do this:
 
```
require('co')(function*() {
  var a = yield asyncFunction1.bind(null, param1)
  var b = yield asyncFunction2.bind(null, param2)
  console.log(a, b)
})
```
 
And that would work as you'd expect.
 
Also, ES7 async/await deserves mentioning.
 
 
04.05.2015, 18:12, "ami" <amin...@gmail.com>:
--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/f2f394a6-80ec-425b-bba3-6ec5f46423ac%40googlegroups.com.

Jan Krems

unread,
May 6, 2015, 9:50:26 AM5/6/15
to nod...@googlegroups.com
Did you know about ES6/ES7 and babel? It allows you to write code like this:

    const readFile = promisify(require('fs').readFile);

    async function f(filename) {
      const content = await readFile(filename);
      return content.trim().length;
    }

I'm not sure another async syntax on top of JavaScript will have a chance of gaining traction at this point, unless there are very compelling reasons.

* The async/await ECMAScript proposal: https://github.com/tc39/ecmascript-asyncawait/

Your project can still be a lot of fun, just mentioning this as a potential alternative. :)

Cheers,
Jan


On Monday, May 4, 2015 at 8:12:13 AM UTC-7, ami wrote:

Adam Reynolds

unread,
May 6, 2015, 9:50:30 AM5/6/15
to nod...@googlegroups.com

This has been solved. It's called promises

--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.

zladuric

unread,
May 7, 2015, 10:24:34 PM5/7/15
to nod...@googlegroups.com
I think you can simply solve this with async, await etc for realworld usages. Even Promises, for most cases.

But if you're doing this as a research or learning project, then I'm curious.

How do you handle the async stuff in the background? Do you use anything from above? How do you manage errors?


On Monday, May 4, 2015 at 5:12:13 PM UTC+2, ami wrote:

Axel Kittenberger

unread,
May 7, 2015, 10:24:46 PM5/7/15
to nodejs
The term "promises" has been so overloaded with so much, simply saying it is not going to help anyone achieve anything.

PS: I'm currently quite happy with this: https://github.com/jmar777/suspend

Reply all
Reply to author
Forward
0 new messages