Search code examples
javascriptthrow

Return function result with "throw" is this correct way to use throw?


One of my colleagues send me this code block:

export const getFieldChoice = (key, listName, fieldName) => {
  const value = cache.get(key) || { status: "new", data: null }

  if (value.status === "resolved") {
    return value.data
  }
  const data = spApi.lists.getByTitle(listName).fields.getByInternalNameOrTitle(fieldName).select('Choices').get().then(x => {
    value.data = x
    value.status = "resolved"
    cache.set(key, value)
  })
  throw data
}

Then I saw that he returns the promise data with throw. Is this the correct way to use throw?


Solution

  • There're a couple of issues with the code and your understanding of the code...

    1. data is not the date returned by the promise. It is the promise. You can only get the data of a promise when the promise resolves. In this case, the data of the promise is actually x
    2. Using the throw keyword will always cause an exception, which if not handled, will cause an error. In this case, the function will always throw an exception, so I'm not sure what the goal here is.

    To actually get the value of the promise within this function there're two ways. Either you await the promise or supply a callback function to be called when the promise resolves...

    Async/Await method

    export const getFieldChoice = async (key, listName, fieldName) => {
      const value = cache.get(key) || { status: "new", data: null }
      if (value.status === "resolved") return value.data;
    
      const data = await spApi.lists.getByTitle(listName).fields.getByInternalNameOrTitle(fieldName).select('Choices').get();
    
      value.data = data;
      value.status = "resolved"
      cache.set(key, value)
    
      return value;
    }
    

    and then it's used like this

    import { getFieldChoice } from "path/to/file";
    
    async function caller () {
      try {
        const key = "key";
        const listName = "list name";
        const fieldName = "field name";
    
        const data = await getFieldChoice(key, listName, fieldName);
        // Rest of function logic
      } catch (err) {
        // Handle any error
      }
    }
    

    Callback method

    export const getFieldChoice = (key, listName, fieldName, callback) => {
      const value = cache.get(key) || { status: "new", data: null }
      if (value.status === "resolved") return value.data;
    
    
     spApi.lists.getByTitle(listName).fields.getByInternalNameOrTitle(fieldName).select('Choices').get().then(data => {
        value.data = data;
        value.status = "resolved"
        cache.set(key, value)
    
        callback(null, value);
      }).catch(err => {
        callback(err);
      });
    }
    

    and then it's used like this

    import { getFieldChoice } from "path/to/file";
    
    function caller () {
      const key = "key";
      const listName = "list name";
      const fieldName = "field name";
    
      getFieldChoice(key, listName, fieldName, (err, value) => {
        if (err) {
          // Handle any error
        }
    
        // Rest of function logic
      });
    
    }
    

    Hope this helps...