Mike Upjohn
Mike Upjohn

Reputation: 1297

Is this the correct way to deal with a JavaScript promise?

Forgive me but I am new to JS promises. I think I need to use one for interacting with an AWS service, which writes to and pulls data from DynamoDB. I have a JavaScript app which is ran by the Serverless npm plugin, which defines exported functions as end points. In these endpoints after the promise has completed I need to bubble the data back up to the endpoint to expose it as a JSON body. See the code below.

exports.getBlog = async (event) => {
    return getBlogPost(event).then((data) => {
        console.log("worked", data);
        var response =  {
            statusCode: 200,
            body: JSON.stringify(data)
        };

        return response;
    })
    .catch((error) => {
        console.log("didn't work");
        var response = {
            statusCode: 400,
            body: JSON.stringify(error.toString())
        };

        return response;
    });
}

What makes me think it's not right is that I have to create a var response and return it, and then return again outside of that in the root of exports.getBlog. Is this right? It makes the JSON print correctly, but am confused from reading tutorials online as to whether this is good practice?

If not, how would you return data from the promise and expose it as a JSON result?

In this example, exports.getBlog is referenced by the Serverless framework as an endpoint, like so:-

functions:
  get-blog:
    handler: api/blog.getBlog
    events:
      - http:
          path: api/v1/blog
          method: GET
          cors: true

Upvotes: 0

Views: 82

Answers (2)

ihor.eth
ihor.eth

Reputation: 2420

You are mixing the two. Here is with async/await

 exports.getBlog = async (event) => {
 try {
    var res = await getBlogPost(event);
    var data = res.data;
    console.log("worked", data);
    var response =  {
        statusCode: 200,
        body: JSON.stringify(data)
    };

    return response;
} catch(error) {
    console.log("didn't work");
    var response = {
        statusCode: 400,
        body: JSON.stringify(error.toString())
    };

    return response;
   }
}

and without

exports.getBlog = event => {
    return getBlogPost(event).then((data) => {
    console.log("worked", data);
    var response =  {
        statusCode: 200,
        body: JSON.stringify(data)
    };

    return response;
})
.catch((error) => {
    console.log("didn't work");
    var response = {
        statusCode: 400,
        body: JSON.stringify(error.toString())
    };

    return response;
});
}

Good read: https://hackernoon.com/6-reasons-why-javascripts-async-await-blows-promises-away-tutorial-c7ec10518dd9

EDIT: adding MDN's article on async/await and how one can rewrite promise code with it

Upvotes: 1

sohrab.slha
sohrab.slha

Reputation: 21

There are two main methods to write promise first using resolve and reject functions and second using .then and .catch function.

First case example:

let promise = new Promise(function(resolve, reject) {
  setTimeout(() => reject(new Error("Whoops!")), 1000);
});

// reject runs the second function in .then
promise.then(
  result => alert(result), // doesn't run
  error => alert(error) // shows "Error: Whoops!" after 1 second
);

Second case example:

If we’re interested only in successful completions, then we can provide only one function argument to .then:

let promise = new Promise(resolve => {
  setTimeout(() => resolve("done!"), 1000);
});

promise.then(alert); // shows "done!" after 1 second

If we’re interested only in errors, then we can use null as the first argument: .then(null, errorHandlingFunction). Or we can use .catch(errorHandlingFunction), which is exactly the same:

let promise = new Promise((resolve, reject) => {
  setTimeout(() => reject(new Error("Whoops!")), 1000);
});

// .catch(f) is the same as promise.then(null, f)
promise.catch(alert); // shows "Error: Whoops!" after 1 second

Upvotes: 1

Related Questions