olavea / Lillian-and-Ola-Vea-com

Lillian-and-Ola-Vea-com
1 stars 0 forks source link

async await and not callback #28

Open olavea opened 2 years ago

olavea commented 2 years ago

See PR with commit here

// /gatsby-node.js

// added async
const createCloudinaryNodes = async (gatsby, cloudinary, options) => {
  // added await
  await cloudinary.api.resources(options, (error, result) => {
    const hasResources = result && result.resources && result.resources.length;
    console.log("This is the result", result);
    if (error) {
      console.error(error);
      return;
    }

    if (!hasResources) {
      console.warn(
        "\n ~Yikes! No nodes created because no Cloudinary resources found. Try a different query?"
      );
      return;
    }

    result.resources.forEach((resource) => {
      const transformations = "q_auto,f_auto"; // Default CL transformations, todo: fetch base transformations from config maybe.

      resource.url = addTransformations(resource, transformations);
      resource.secure_url = addTransformations(resource, transformations, true);

      const nodeData = getNodeData(gatsby, resource);
      gatsby.actions.createNode(nodeData);
    });

    console.info(
      `Added ${hasResources} CloudinaryMedia ${
        hasResources > 1 ? "nodes" : "node"
      }`
    );
  });
};

Looks like it worked ⬇️

gatsby-cloudinary-week_1_result await_5

raae commented 2 years ago

Det er fortsatt callbacken som gjør arbeidet, alt som liksom er inne der skal utenfor:

const result = await cloudinary.api.resources(options)
// her kommer alt arbeidet som før skjedde inn i callbacken