DEV Community

Discussion on: Jordan promises – async/await vs .then

Collapse
 
johncarroll profile image
John Carroll • Edited

Async/await is obviously better (in most cases) then .then() style promise handling, but your first example of "Yucky then usage" seems unnecessarily bad. Unless I am missing something, there is no apparent reason for the code to be implemented that way (other than inexperience).

You could argue that one of the benefits of async/await is that it's harder for an inexperienced programmer to use the syntax improperly, but you haven't made that argument.

That same example can be implemented much more cleanly/realistically (while still using .then())

function getThePizza() {
  let someData;

  return functionGettingFromMongo('someArguments')
    .then(data => {
      someData = data;
      return doSomeWorkWithAPromise();
    })
    .then(data => insertToMongoNow(someData + data))
    .then(response => { console.log('success!', response) })
    .catch(e => { console.error('some error happened', e) })
}

While .then() isn’t a callback function, codewise it reads the exact same.

False. It only reads that way because of a poor implementation.