0

I am beginner at javascript so please bear with me. I wonder how to put async() event in the right way. I have 2 code snippets that I want to execute asynchronously and not synchronously. The code snippets use a library that do HTTP requests so that is out of my control.

So I like the 2 code snippets to execute in parallell somehow. What I have are those 2 code snippets and I also think I understand that I only want to declare the first 2 lines once as those lines takes time:

'use strict';
 const ccxt = require ('ccxt');

The 2 code snippets are the below

Code snippet1:

'use strict';
const ccxt = require ('ccxt');

(async () => {
            try{
       const exchange = new ccxt.one({ enableRateLimit: true })
       const tickers = await exchange.fetchTickers()
       const obj = { tickers }
       const fs = require('fs');
       fs.writeFile("/myproject/file1.txt", JSON.stringify(obj), function(err) { });
       }catch{}
}) ()

Code snippet2:

'use strict';
const ccxt = require ('ccxt');

(async () => {
            try{
       const exchange = new ccxt.two({ enableRateLimit: true })
       const tickers = await exchange.fetchTickers()
       const obj = { tickers }
       const fs = require('fs');
       fs.writeFile("/myproject/file2.txt", JSON.stringify(obj), function(err) { });
       }catch{}
}) ()
gosuto
  • 5,422
  • 6
  • 36
  • 57
Andreas
  • 1,121
  • 4
  • 17
  • 34

3 Answers3

1

I tried this code and it actually did it in parallell. It executed very fast.

If you have any idéas of code to add to make it even more efficient I would be very happy to hear how to do that. (For example open up more ports or any other bottlenecks?)

'use strict';
const ccxt = require ('ccxt');

(async () => {
            try{
       const exchange = new ccxt.one({ enableRateLimit: true })
       const tickers = await exchange.fetchTickers()
       const obj = { tickers }
       const fs = require('fs');
       fs.writeFile("/myproject/file1.txt", JSON.stringify(obj), function(err) { });
       }catch{}
}) ();

(async () => {
            try{
       const exchange = new ccxt.two({ enableRateLimit: true })
       const tickers = await exchange.fetchTickers()
       const obj = { tickers }
       const fs = require('fs');
       fs.writeFile("/myproject/file2.txt", JSON.stringify(obj), function(err) { });
       }catch{}
}) ();
Andreas
  • 1,121
  • 4
  • 17
  • 34
0

Use Promise.all:

'use strict';

const ccxt = require ('ccxt')
const fs   = require('fs')

async function work (exchangeId) {
    try {
        const exchange = new ccxt[exchangeId] ({ enableRateLimit: true })
        const tickers = await exchange.fetchTickers ()
        const obj = { tickers }
        const filename = exchangeId + '.txt'
        fs.writeFileSync (filename, JSON.stringify (obj))
        console.log ('saved', filename)
    } catch {
    }
}

(async () => {
    const exchangeIds = [ 'bittrex', 'bitfinex' ]
    await Promise.all (exchangeIds.map (exchangeId => work (exchangeId)))
}) ()
Igor Kroitor
  • 1,548
  • 13
  • 16
0

It's not clear to me what you want to happen but your code will not catch all errors as written. I know you seemed to be ignoring all errors but just in case, ...

If you're going to use async/await then you should go all in. That means you should use fs.promises.readFile not fs.readFile. Either that or you should wrap fs.readFile in a promise either manually or using util.promisify.

So the code becomes

'use strict';
const ccxt = require ('ccxt');

const thing1 = (async () => {
   try{
       const exchange = new ccxt.one({ enableRateLimit: true })
       const tickers = await exchange.fetchTickers()
       const obj = { tickers }
       const fs = require('fs');
       await fs.promises.writeFile("/myproject/file1.txt", JSON.stringify(obj));
   } catch {
      // catch error here
   }
}) ();

const thing2 = (async () => {
    try{
       const exchange = new ccxt.two({ enableRateLimit: true })
       const tickers = await exchange.fetchTickers()
       const obj = { tickers }
       const fs = require('fs');
       await fs.promises.writeFile("/myproject/file2.txt", JSON.stringify(obj));
    } catch {
      // catch error here
    }
}) ();

If wanted to do both to wait for both things then you could use Promise.all by passing in an array that contain each of the promises returned by both async functions.

'use strict';
const ccxt = require ('ccxt');

const thing1 = (async () => {
   try{
       const exchange = new ccxt.one({ enableRateLimit: true })
       const tickers = await exchange.fetchTickers()
       const obj = { tickers }
       const fs = require('fs');
       await fs.promises.writeFile("/myproject/file1.txt", JSON.stringify(obj));
   } catch {
      // catch error here
   }
}) ();

const thing2 = (async () => {
    try{
       const exchange = new ccxt.two({ enableRateLimit: true })
       const tickers = await exchange.fetchTickers()
       const obj = { tickers }
       const fs = require('fs');
       await fs.promises.writeFile("/myproject/file2.txt", JSON.stringify(obj));
    } catch {
      // catch error here
    }
}) ();

(async() => {
  await Promise.all([thing1, thing2]);

  // do something after thing1 and thing2
}) ();

And of course at given the 2 functions are the same except for the filename then

'use strict';
const ccxt = require ('ccxt');

async function fetchTickersAndWrite({method, filename}) {
   try{
       const exchange = new ccxt[method]({ enableRateLimit: true })
       const tickers = await exchange.fetchTickers()
       const obj = { tickers }
       const fs = require('fs');
       await fs.promises.writeFile(filename, JSON.stringify(obj));
   } catch {
      // catch error here
   }
}

(async() => {
  await Promise.all([
    { method: 'one', filename: `/myproject/file1.txt` },
    { method: 'two', filename: `/myproject/file2.txt` },
  ].map(fetchTickersAndWrite));

  // do something
}) ();
gman
  • 100,619
  • 31
  • 269
  • 393