N
N
Nick Fisher2020-04-23 00:21:01
JavaScript
Nick Fisher, 2020-04-23 00:21:01

How does executor work in new Promise?

Good evening. I got a little unusual code for me, where the syntax is actively used

new Promise(executor =>  {
  if (....) {
    ....;
    executor();
    return; 
  } else if (....) {
    ....;
    ....;
    executor();
  }
  if (....) {
    ....;
    ....;
   executor()
   return()
  }
})


I read various materials, but still I would like a deeper understanding.
Can you explain what exactly happens when it comes to executor()? Does the promise resolve and the call terminates? What if the conditions of several ifs are met (in the function that returns a promise, several arguments are passed, which should cause several ifs)? Thanks in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2020-04-23
@Nick Fisher

a function is passed to new Promise (in this case, an arrow function).
It has two parameters - a function for resolve and a function for reject.
In this case, the second parameter is not specified (reject is not used here), and the first one is named executor. And in the right places of our arrow function, the executor function is called to resolve the promise. In a more familiar code, it would be called resolve or res, but in general, you can call this parameter whatever you like, the main thing is that it should go first in the list

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question