News for hackers. Stuff that matters.
Re:JavaScript Promises Discussion: Make Them Monadic? (2013) by tachang on Thursday February 15, 2018 @07:35PM (#16389025)
Can you explain a bit more what your code is doing?

Are you suppose to instantiate the handler class and call it?

Re:JavaScript Promises Discussion: Make Them Monadic? (2013) by nawgszy on Friday February 16, 2018 @02:40PM (#16395324)
Yeah, exactly. Say you have an API request that is persisting a chunk of data to the database, like a few dependent data items.

This way, you write the API request to just be a simple function invocation of the class you instantiated:

  (req, res, next) => {
    apiHandler = new APIHandler(args);

    apiHandler.saveData(req.body)
    .then(
      // success
      () => res.status(200).json(apiHandler.getResponse()),
      // error
      () => res.status(400|500).json(apiHandler.getErrorResponse())
  )
I mean, it's just a pattern, and I wouldn't necessarily say I use it all the time; it's especially valuable for workers, where you have a series of functions you want to execute where some of them are going to be asynchronous.

If you're just making simple create / update / get requests, it's less valuable, and you can just return a Promise from somewhere. Hopefully that gave you an idea of my motivations