Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

135
Views
pure JavaScript Promise doesn't work as expected

I use Node.js in server, and want to run 1 to 5 as expected in code below. But in console log, I get '1, 4, 5, 2, 3'.

the invoke_follow function variable result starts when Promise object constructed after console.log(1).

Between console.log(2) and console.log(3), using request module to get information from API takes about 100ms. So that, I want wait console.log(4) until 2 and 3 has come.

Sorry for weak English. Thank you for advance.

Could you please give me why.


const request = require('request');
 
function invoke_follow(){
    
    const optionses = {
      url: 'DestinationUrl.com', 
      method: 'POST'
    };
    
      console.log("2");
      
    request(optionses, function (error, response, body) {
    
        console.log("3");
    });

    console.log("4");
  }

function promise_test (){
    
    let result = invoke_follow;
    console.log("1")
    
    let promise = new Promise(result);
    console.log("5")
    return promise;
  }

promise_test();
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!