var cordinats = function () { return new Promise(function(resolve, reject){ if(navigator.geolocation) { navigator.geolocation.getCurrentPosition(function(position) { var long = position.coords.longitude, lat = position.coords.latitude; var myLocation = { long: long, lat: lat }; console.log('Оп1'); resolve(myLocation); console.log(myLocation); }) } var errObj = { 0: "Unkonown Error", 1: "Permission denied by User", 2: "Position is not available", 3: "Request Timed Out" } console.log('Оп2'); reject(`${err.code} ${errObj[err.code] }`); }); } cordinats().then(function(location) { var loc = location; console.log(loc); changeHtmlInfo(); }, function(error) { }); 

I open the code through Chrome. At first, Op2 appears in the console. Then, when confirming data on geolocation readout - Op1. The last console.log (loc) is not executed at all. Question:

  1. why such a sequence of code execution
  2. Why not cordinats().then
  • Six code scrolls are hard - Dmitriy Simushev
  • 2
    As the moderators usually write, “The question doesn’t show the work of thought and the desire to understand the problem.” Moreover, you didn’t even write what your code does, why it is needed at what stage the error occurs, and I'm sorry 6 nobody will read from pure altruism - Broouzer King
  • one
    @Evander it will not be executed, since function(error) { console.log("UPS... Reject") } will be executed much earlier. It looks like getCurrentPosition asynchronous and while it reaches it, the cancellation will occur earlier. - Alex Krass
  • one
    Because console.log (loc); not performed. - in the then method there are two parameters: the first is responsible for the successful resolution of the promise (resolve), the second is responsible for the unsuccessful (reject). Since you output Оп2 , you call reject(...); and you smoothly transfer to the function that you passed by the second parameter to then . Since it is empty, you don’t see anything - Grundy
  • one
    Well, in addition, the logic is wrong: you always do a reject first, even if you entered an if , but since inside if operation is asynchronous, its callback will be executed after the completion of the main function - Grundy

1 answer 1

Why didn't it work?

The getCurrentPosition method is asynchronous. This means that when entering the if execution does not stop until the callback is executed, but passes on, exits the if and calls reject .

The then method takes two parameters: the first for successful resolution and the second for not successful.

Since reject is always called, the function passed to the second parameter is always called, and since it is empty, nothing was output.

How to fix?

For example, you can get rid of if and use the fact that the getCurrentPosition method getCurrentPosition also accept a handler for an error. Now the function is always called and the completion of the Promise depends on calling the appropriate handler, it is enough just to call resolve and reject in the right places.

 function cordinats() { return new Promise(function(resolve, reject){ navigator.geolocation.getCurrentPosition(function(position) { var long = position.coords.longitude, lat = position.coords.latitude; var myLocation = { long: long, lat: lat }; resolve(myLocation); }, function(err) { var errObj = { 0: "Unkonown Error", 1: "Permission denied by User", 2: "Position is not available", 3: "Request Timed Out" } reject(`${err.code} ${errObj[err.code] }`); }); }); } cordinats().then(function(location) { var loc = location; console.log(loc); changeHtmlInfo(loc); }, function(error) { console.log(error); }); 
  • one
    write down in more detail, especially since you didn’t go there. return added :-) - Grundy
  • @Grundy OK I removed return resolve(myLocation); in this line return resolve(myLocation); return. Everything is working. But now I do not understand how the code is generally executed. Promises are used for asynchronous code execution, I understand that. I do not understand in what sequence this code is executed. I read about promises, but so far my knowledge of js is not so deep as to understand the example in that article [ learn.javascript.ru/promise] . There are those who here, using the example of my code, will explain in what sequence the code is executed ? - Evander
  • Edited your answer. - Grundy