NodeJS script not actioning with async/await

3986 views javascript
5

Cannot figure out why the below script won't run. It is likely the script is not going to do what I want but using

node ./contentful/contentful-assets.js

in the terminal, it does nothing - No errors, nothing logged for me to even start debugging. However, if I remove async it will attempt the script and shoot back an error.

./contentful/contentful-assets.js

const contentful = require('contentful-management');
const iterator = require('make-iterator');
const assets = require('./assetObject.js');

async resolve => {
  console.log('Creating Contentful client');
  const client = contentful.createClient({
    accessToken: 'token',
    logHandler: (level, data) => console.log(`${level} | ${data}`)
  });

  const iterableAssets = iterator(assets);
  const space = await client.getSpace('space');
  const environment = await space.getEnvironment('enviroment');

  const cmsAssets = [];

  const assetProcessingTimes = [];
  const inProcess = new Map();
  let processedAssetsCounter = 0;

  const createAndPublishSingleAsset = async ({ asset, done, index }) => {
    if (done) {
      if (inProcess.size > 0) return false;
      return resolve(cmsAssets);
    }

    const start = Date.now();
    const id = '' + start + Math.round(Math.random() * 100);
    inProcess.set(id, true);
    let cmsAsset;

    try {
      cmsAsset = await environment.createAssetWithId(asset.postId, {
        fields: {
          title: {
            'en-US': asset.title
          },
          description: {
            'en-US': asset.description
          },
          file: {
            'en-US': {
              contentType: 'image/jpg',
              fileName: asset.filename,
              upload: asset.link
            }
          }
        }
      });
    } catch (e) {
      console.log(`Asset "${asset.title}" failed to create, retrying...`);
      createAndPublishSingleAsset({
        asset,
        done,
        index
      });
    }
    try {
      const processedCMSAsset = await cmsAsset.processForAllLocales();
      const publishedCMSAsset = await processedCMSAsset.publish();

      cmsAssets.push(publishedCMSAsset);
      assetProcessingTimes.push((Date.now() - start) / 1000);
      inProcess.clear(id);
      const eta = Math.floor(
        assetProcessingTimes.reduce((a, b) => a + b, 0) /
          assetProcessingTimes.length *
          (assets.length - index) /
          60
      );
      processedAssetsCounter += 1;
      console.log(
        `Processed asset ${processedAssetsCounter}/${assets.length} - eta: ${eta}m`
      );
      createAndPublishSingleAsset(iterableAssets.next());
    } catch (e) {
      console.log(`Asset "${asset.title}" failed to process, retrying...`);
      await cmsAsset.delete();
      createAndPublishSingleAsset({
        asset,
        done,
        index
      });
    }
  };
  console.log('Starting to create assets');
  createAndPublishSingleAsset(iterableAssets.next());
  createAndPublishSingleAsset(iterableAssets.next());
  createAndPublishSingleAsset(iterableAssets.next());
};

assetObject.js

[
  {
    link: 'https://example.com/example1.jpg',
    title: 'Example 1',
    description: 'Description of example 1',
    postId: '1234567890',
    filename: 'example1.jpeg'
  }, ... // Many more
]

What have I missed here?

answered question

1 Answer

1

I fear that you are not calling the function, could you try, the following?

const contentful = require('contentful-management');
const iterator = require('make-iterator');
const assets = require('./assetObject.js');

const work = async resolve => {
  console.log('Creating Contentful client');
  ...
}

work();

I think that you are just declaring a function, but not actually calling it, just felling

posted this

Have an answer?

JD

Please login first before posting an answer.