0

I'm using a framework where I found code like this:

block1

fun_1(params, callback) { 
        fun_2(params, callback) {
                ...     
                     fun_n(params, callback) {
                          asyncFunction().then(callback).catch(callback)
                                }

as asyncFunction is from a deprecated npm package i would take the opportunity to refactor it. I would like to switch to something like this:

block2

fun_1(params).then(callback)

Where fun_1 would be:

fun_1(params) {
    fun_2(params) {
          return asyncFunc()     ???
  }
}   

Is the second pattern correct and preferable over the first ?

Nja
  • 439
  • 6
  • 17
  • Is this on the frontend, or in Node? – CertainPerformance Oct 21 '20 at 14:28
  • Im just learning Async functions, but you shoudint use promisses? Like `return new Promisse(resolve=>{ . . .` – Luís HNrique Oct 21 '20 at 14:30
  • This is on `node` @CertainPerformance , i suppose asyncFunction returns a Promise yes – Nja Oct 21 '20 at 14:31
  • @LuísHNrique when you are using async, you should stop dealing with Promises directly or you create ugly code. async is an abstraction to hide Promises. – Gazihan Alankus Oct 21 '20 at 14:51
  • This is not syntactically valid code, so we're not sure what you're dealing with here. In general, you might want to follow my [rules of thumb for working with promises](https://stackoverflow.com/a/25756564/1048572). – Bergi Oct 21 '20 at 22:12
  • Thank you @Bergi that was exactly what i was looking for! Where does those best practices (3 rules) come from, you're experience or any reference? In any case they seems to me bulletproof i will surely follow that. – Nja Oct 22 '20 at 07:17
  • @Nja Let's say [experience](https://stackoverflow.com/tags/promise/topusers), yes :-) – Bergi Oct 22 '20 at 08:09
  • @Bergi greeeeat! – Nja Oct 22 '20 at 08:51

2 Answers2

0

It seems correct. However, all functions need to return that promise when they call the inner function. For example:

fun_1(params) {
  fun_2(params) {
    return asyncFunc();
  }

  return fun_2(params);
} 

fun_1(params).then(callback);
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Gazihan Alankus
  • 11,256
  • 7
  • 46
  • 57
  • Thank you @Gazihan Alankus! Is **block2** a best practice or there are any advantages on **block1** . **block1** makes the code less readable as to know what will happen i can't read lineary the code but need to open the code of all the nested functions (sometimes scattered all over some different files). Does it speed the returning from `fun_n` to `fun_1` calling immediately callback in `fun_n` ? – Nja Oct 21 '20 at 17:21
  • If there would be any gains in speed by calling it from `fun_n` before returning from it, it would be negligible. I think not littering the code with the callback parameter everywhere is a good approach. Also, if the function names indicate that they are starting something, callers can be certain that it's returning a promise and prepare themselves. – Gazihan Alankus Oct 22 '20 at 05:41
0

There's not much information in your question, but I'll try to answer with what I have.

In order for fun_1 to be able to be chained with .then, it needs to return a promise:

function fun_1() {
   return new Promise((resolve, reject) => {
      // Do some tasks and resolve when complete
      resolve(/* some data */)
   }
}

Or, by using the async keyword, which is just syntactic sugar for Promise:

async function fun_1() {
    await some_async_task()
    return; // does the same as resolve would in Promise
}

In order to refactor the code, you're going to have to move the async stuff and the callbacks in promises. Let me know if you want to clarify some things in your question to get a better answer.

  • Hi @Jacob , thank you. `fun_1` calls, after some other function, a `asyncFunction`. Doesn't `asyncFunction` already returns a `promise` ? What i would like to know is if i should or not return some `Promise` from `fun_1` or it is already done by `asyncFunction`. And if **block2** is the best practice. – Nja Oct 21 '20 at 17:15
  • I don't understand where fun_2 is called. Is fun_1 supposed to return the result of fun_2? If asyncFunction returns a promise, you _can_ just return it. – Jacob Belanger Oct 21 '20 at 17:47