V
V
Vann Damm2020-03-09 11:22:12
PHP
Vann Damm, 2020-03-09 11:22:12

Why is then needed in promises if there is await?

Why use then in promises? if it does not wait for the result of the query execution in the promise?, for example, 10 seconds. Does it throw a promise into then in the Pending status?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
T
Tweedledum, 2019-07-29
@l_mke

strpos($_SERVER['REQUEST_URI'], '/services/') !== false ? 'header' : false

S
Sergey Sokolov, 2020-03-09
@sergiks

“In the beginning there were promises ”
Then, later, they made a multi-colored syntactic wrapper for them - async / await
For promises, the next promise is the holder of the “pause” button. This promise, like an alarm clock, can call a method then()- how to stick a sticker on the dial, where it says what needs to be done when the time comes. Nothing gets "immediately" into the
glued method . then()Rather, 2 functions are immediately passed to it: one will be called when the promise is fulfilled; the second - if it breaks off: .then(onResolve, onError)
There is an alarm clock, there is a then sticker on it, it says what to do if OK and if it's a bummer. Wait-sir. It's ticking.
awaithides extra wires under the hood of the JS engine, and at the top simply returns the result of the performed async. Or throws an Exception in case of a break - it must be caught with the usual ones . After re-reading the try..catch
question, I will assume that the promise with the request was prepared incorrectly . It should be something like this:

const requestPromise = new Promise(function(res, rej) {
  setTimeout( function(result){ // имитация асинхр запроса куда-то
    // тут, типа, наконец получили ответ
    if (result.error_code) {
      rej(error_code); // облом
    } else {
      res(result); // обещание выполнено с результатом
    }
  }, 2000);
});

// здесь requestPromise – это Promise в статусе "pending"

requestPromise.then( // сюда ничего не попадает «сразу». Оно внутри ждёт. Терпеливо.
  function(result) {console.log(result, "мы молодцы");},
  function(error) {console.error(error, "облом вышел");},
);
If you wish, you can add more then dogs to the same Promise:
requestPromise.then(r => console.log("result log:", r));

A
abberati, 2020-03-09
@abberati

You can wait until a promise is fulfilled either with then or with await . Await does pretty much the same thing as then. Only await is a syntactic feature, and then is a semantic one. If you need clarification, please ask.

F
Flying, 2020-03-09
@Flying

Answering the question in the title: Async functions (the same one async/await) appeared in the ECMAScript specification only in the ES2017 version , Promisebut it appeared much earlier.
Answering the question in the text: then()it is called at the moment when the Executor (the function passed to Promise) calls the callback passed to it for resolve. In general, this is, as it were, the very base of use Promise, so perhaps you should refresh your knowledge by reading the documentation .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question