0

I am trying to understand the logic of promises here. But I can't wrap my head around the code.

Can someone help me understand?

What does (r)=> fn = r, means in this code.

function promise(a,b) {

  return new Promise((resolve,reject) => {
      if(a%2 !== 0) {
        reject('ODD ');
        return;
      } 
      resolve('even');

  });

}

let list_of_promise = [ promise(1),promise(2)
,promise(2),promise(2),promise(2),promise(2)
,promise(2),promise(2),promise(2),promise(2)
,promise(2),promise(2),promise(2),promise(2),promise(2)
,promise(2),promise(2),promise(2),promise(2)
,promise(2)];



function listOfPromise(list_of_promise) {

  let arr = [];
  let fn;
  let count = 0;

  list_of_promise.map( (p) => {
    p.then((res)=>{
      arr.push(res);
      arr.length === 10 && fn(arr);
    });
  })

  return new Promise((r,rj) => fn= r);
}

listOfPromise(list_of_promise).then((res)=>{
  console.log("result set ", rest);
});
user583726
  • 647
  • 2
  • 9
  • 20

1 Answers1

1

This is horrible code.

fn = r is just assigning the resolve() function from the new Promise() executor to a higher scope so it can be called from outside the executor, causing the promise to resolve. This is a very obtuse way to write the code. A design pattern for resolving a promise from the outside is typically referred to as a Deferred. 99.999% of the time, there is no need for the Deferred pattern and there's a bunch of good reasons why it was not built into the promise architecture. If you want to see a simple Deferred object, you can see here and here.

To rewrite this in a less obtuse way, I would need to understand what the objective of the code is (in terms of a real-world problem) so I could suggest the best way to solve the actual problem. Right now, it looks like demo code trying to demonstrate something, not trying to solve a real world problem. I prefer to focus coding on real world problems rather than theoretical discussions as the real world problem provides priorities for the actual coding strategy.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • Let's say in the real-world I am building a photoshop app in browser, User kicks in a process in browser which will start 100/1000 operations that need to be sent on the backend side, and later when the response comes back I need to reflect that in the browser. – user583726 Jun 13 '20 at 01:19
  • @user583726 - That's just a normal use of promises. None of these obtuse mechanisms are needed for that. If you want to know when the first promise of a group finishes, you use `Promise.race()`. If you want to know when they all finish, you use `Promise.all()`. Other permutations can be built by combining the promises using logic. I'm happy to make a suggestion for a real-world problem with code shown as I have done thousands of times here. Without that often requires teaching the generality of how to use promises in a wide variety of situations which is beyond what one answer can do here. – jfriend00 Jun 13 '20 at 02:01