0

nodejs version v10.14.2 OS win7 sp1 64bit

I need the following four lines of code to execute in order

var res2 = await fetchData(url_2, file_path_2);
await console.log('step one.');
var res1 = await fetchData(url_1, file_path_1);
await console.log('step two.');

However, the actual order of their execution is

step one. step two. ........ ........

How can we achieve synchronous execution of these four lines of codes?

var https = require('https');
var fs = require('fs');
var request = require('request');
var async = require("async");

(async() => {

var url_1 = 'https://aaa.bbb.ccc.com';
var url_2 = 'https://library.harvard.edu/';

var file_path_1 = fs.createWriteStream('./intelcenter/aaa.bbb.ccc.txt');
var file_path_2 = fs.createWriteStream('./intelcenter/harvard.txt');

var res2 = await fetchData(url_2, file_path_2);
await console.log('step one.');
var res1 = await fetchData(url_1, file_path_1);
await console.log('step two.');

})();


async function fetchData(url,locpath) {
  return new Promise(function (resolve) {
    async.retry(request(url).pipe(locpath), function(err, result) {
    resolve(result);
    });   
 });
}

1 Answer 1

1

Synchronous Code

I have added callback function for request module and passing the result via next method.

By Default async.retry will make 5 retry.

var fs = require("fs");
var request = require("request");
var async = require("async");

(async () => {
  var url_1 = "https://google.com";
  var url_2 = "https://library.harvard.edu/";

  var file_path_1 = fs.createWriteStream("./intelcenter/aaa.bbb.ccc.txt");
  var file_path_2 = fs.createWriteStream("./intelcenter/harvard.txt");

  var res2 = await fetchData(url_2, file_path_2);
  console.log("step one." + res2);
  var res1 = await fetchData(url_1, file_path_1);
  console.log("step two." + res1);
})();

function fetchData(url, locpath) {
  console.log("inside fetchData");
  return new Promise(function(resolve, reject) {
    async.retry(
      function(next) {
        request(url, function(err, result) {
          if (err) {
            console.log(err);
          }
          console.log("inside request");
          next(result);
        }).pipe(locpath);
      },
      function(result) {
        resolve(result);
      }
    );
  });
}

To know more about async.

Synchronous Code without async.retry

var fs = require("fs");
var request = require("request");

(async () => {
  var url_1 = "https://google.com";
  var url_2 = "https://library.harvard.edu/";

  var file_path_1 = fs.createWriteStream("./intelcenter/aaa.bbb.ccc.txt");
  var file_path_2 = fs.createWriteStream("./intelcenter/harvard.txt");

  var res2 = await fetchData(url_2, file_path_2);
  console.log("step one.");
  var res1 = await fetchData(url_1, file_path_1);
  console.log("step two.");
})();

function fetchData(url, locpath) {
  console.log("inside fetchData");
  return new Promise(function(resolve, reject) {
    request(url, function(err, result) {
      if (err) {
        console.log(err);
      }
      console.log("got result");
      result=JSON.stringify(result);
      locpath.write(result);
      resolve(result);
    });
  });
}
Sign up to request clarification or add additional context in comments.

4 Comments

You should explain what you changed and why.
I have removed async.retry, I am writing the result to the file inside the callback function of request module.
@Brad I have modified the answer to specifically to this question
The async api , At first, I don't understand the meaning of apiMethod.,now is replaced by function(next) async.retry({ times: 10, interval: function(retryCount) { return 50 * Math.pow(2, retryCount); } }, apiMethod, function(err, result) { // do something with the result });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.