1

I've been reading about async and await in JS and tried to implement them in my code (which I totally messed up).

Here is my JS.

var express = require('express');
var router = express.Router();
var jsforce = require('jsforce');
const SEC_TOKEN = 'SEC_TOKEN';
const USER_ID = 'USER_ID';
const PASSWORD = 'PWD';
const { default: axios } = require('axios');

router.get("/", async (req, res, next) => {

  await initConnect;
  await soqlData;
  await slackPostTest;
});

initConnect = async () => {
  var conn = new jsforce.Connection({
    loginUrl: 'https://login.salesforce.com'
  });
  await conn.login(USER_ID, PASSWORD + SEC_TOKEN, (err, userInfo) => {
      if (err)
        console.log(err);
      else {
        console.log(userInfo.Id);
      }

    });
}

soqlData = async () => {
  await conn.query('Select Id, Name from Account LIMIT 1', (err, data) => {
      if (err)
        console.log(err);
      else
        return data.records[0];
    })
}

slackPostTest = async () => {
  await  axios.post('SLACK_WEBHOOK', {
      "text": "soqlRes"
    })
}

module.exports = router;

What I am trying to achieve?

  1. Initialize my connection by passing in SEC_TOKEN, USER_ID, PASSWORD to my initConnect function this will give me a connection (conn).
  2. Use this conn and query my salesforce instance and get some other data.
  3. post some message(currently irrelevant, but will hook up with the above response later) to my slack endpoint.

Also can someone please give me a little detailed explanation of the solution (in terms of async/await)?

Thanks

user3872094
  • 3,269
  • 8
  • 33
  • 71
  • You aren't calling any of those async functions in `get()`, so nothing will happen. – AKX Feb 22 '21 at 15:27
  • I don't think you can stick `await` in front of a function which use callbacks and expect it to work. – evolutionxbox Feb 22 '21 at 15:28
  • Additionally, `jsforce` is callback-based, not promise-based, so you would need to promisify it first. – AKX Feb 22 '21 at 15:28
  • Does this answer your question? [How to "await" for a callback to return?](https://stackoverflow.com/questions/37104199/how-to-await-for-a-callback-to-return) – evolutionxbox Feb 22 '21 at 15:29

1 Answers1

3

Assuming everything else about your JsForce API usage was correct (I have no experience with it, so I can't say), here's how to promisify those callback-based APIs and call them.

var express = require("express");
var router = express.Router();
var jsforce = require("jsforce");
const SEC_TOKEN = "SEC_TOKEN";
const USER_ID = "USER_ID";
const PASSWORD = "PWD";
const { default: axios } = require("axios");

router.get("/", async (req, res, next) => {
  const { conn, userInfo } = await initConnect();
  const data = await soqlData(
    conn,
    "Select Id, Name from Account LIMIT 1",
  );
  await slackPostTest(data.records[0]);
});

function initConnect() {
  const conn = new jsforce.Connection({
    loginUrl: "https://login.salesforce.com",
  });
  return new Promise((resolve, reject) => {
    conn.login(USER_ID, PASSWORD + SEC_TOKEN, (err, userInfo) => {
      if (err) return reject(err);
      resolve({ conn, userInfo });
    });
  });
}

function soqlData(conn, query) {
  return new Promise((resolve, reject) => {
    conn.query(query, (err, data) => {
      if (err) return reject(err);
      resolve(data);
    });
  });
}

function slackPostTest(soqlRes) {
  return axios.post("SLACK_WEBHOOK", {
    text: soqlRes,
  });
}

module.exports = router;
AKX
  • 152,115
  • 15
  • 115
  • 172