//Create a promise object
let promise=new Promise((resolve, reject)=>{

    setTimeout(()=>{
        //will transfer the promise to the fulfilled state with the result"result"
        resolve("result");
    }, 1000);

});

//promise.then hangs handlers for a successful result or error
promise
    .then(
        result=>{
            //first handler function - will be launched when calling resolve
            alert("Fulfilled:" + result);//result - the argument argument
        },
        error=>{
            //second function - will start when calling reject
            alert("Rejected:" + error);//error - reject argument
        }
    );


I would like to clarify how asynchrony works in this example.

I correctly understood that this code
setTimeout(()=>{
        //will transfer the promise to the fulfilled state with the result"result"
        resolve("result");
    }, 1000);

after 1 second, it will save the result"result"?

But before it takes 1 second, resolve goes here and waits for the"result" to be saved.And after 1 second passes, it calls an alert with the value of resolve?
.Then(
        result=>{
            //first handler function - will be launched when calling resolve
            alert("Fulfilled:" + result);//result - the argument argument
        },

2 Answers 2

Initially, the promise you created is in the state pending

This code
setTimeout(()=>{
        //will transfer the promise to the fulfilled state with the result"result"
        resolve("result");
    }, 1000);


It is stored in the promise object and will be called immediately, and when `resolve` is called, this call triggers a state change event on the promise object.
Because the resolve is called, the promis changes to fulfilled and the value from resolve is passed to the callback, which will be called inside then.

I'm not sure that I understood the essence of your question correctly, but I hope this can help.

  1. https://learn.javascript.ru/promise
  2. https://habr.com/ru/company/mailru/blog/269465/
  • MTV's94 Tell me if I understood correctly.This code
    setTimeout(()=>{
            //will transfer the promise to the fulfilled state with the result"result"
            resolve("result");
        }, 1000);


    How would"transform" into this type of code and execute?
    setTimeout(()=>{
            //will transfer the promise to the fulfilled state with the result"result"
            resolve(alert("Fulfilled:" +"result");
        }, 1000);
    – Fierce40 Jul 29 '19 at 17:01
javascript obeys the Event Loop model, the script runs"in one thread", in each cycle(tick) of this cycle all pending timers are checked, all event handlers, etc.


//Promise object is created
//nothing happens, just save the object
let promise=new Promise((resolve, reject)=>{
//Tag #1(see below)
//executes setTimeout()
//number 1 in the picture, the code is here
    setTimeout(()=>{
        //number 2 in the picture, in a second(or when the processor has time)
        //JS interpreter will parse the timer queue and execute a callback resolve()
        //thereby"allow" promis
        resolve("result");
    }, 1000);

});

//promise.then hangs handlers for a successful result or error
promise
//GOTO label number 1
    .then(
        result=>{
            //first handler function - will be launched when calling resolve
            //in Timer Queue(possibly) there will be a link to this function, and it will be called with the result argument
            alert("Fulfilled:" + result);//result - the argument argument
        },
        error=>{
            //second function - will start when calling reject
            //in Timer Queue(possibly) there will be a link to this function, and it will be called with the result argument
            alert("Rejected:" + error);//error - reject argument
        }
    );