0

Never used defer before but my agent asked me to use defer with promises.

In mozilla defer page, it says defer is obsolete and should not be used anymore. But in the same page there is example.

My question is, why to use defer with promise? Whats it's advantage for code below, only write less code? I should define a new promise instead of that?

export function defer () {
  const deferred = {}
  deferred.promise = new Promise((resolve, reject) => {
    deferred.resolve = resolve
    deferred.reject = reject
  })

  return deferred
}

And the code that is using it:

export async function waitForPredicate (peer, path, predicate, cancelToken) {
  const deferredFetcher = defer()
  ...
  let result = predicate(data)
  if (result) {
    deferredFetcher.resolve(result)
  }
})
Mamun
  • 66,969
  • 9
  • 47
  • 59
Rasim AVCI
  • 133
  • 2
  • 4
  • 11
  • Who's your "agent"? – Bergi Jan 22 '18 at 13:45
  • I can not give my agent name but its a big one. Actually I am not asking difference between defer().promise and Promise. I am asking why use an old style defer for a promise. – Rasim AVCI Jan 23 '18 at 05:56
  • I am asking you to change your agent - he does not understand promises :-) – Bergi Jan 23 '18 at 07:58
  • I dont think so, he is a master on that. I did not wrote whole story. There is a promise race and one of them is timeout promise. also there is tagPromise function. There must be some reason people still use it. – Rasim AVCI Jan 23 '18 at 10:34

1 Answers1

0

Before the ES6 Promise pattern was standardized, deferreds were a pattern/view into a Promise that can modify the state.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
  • This has nothing to do with ES6 standardisation. – Bergi Jan 22 '18 at 13:46
  • @Bergi i'm trying to give *historical* context here. – Daniel A. White Jan 22 '18 at 14:18
  • Sometimes defer needed when you use them in other class and you dont need use promise. I mean you already have a new promise in defer object and you can use use whenever you want, and you can also use resolve and reject as parameters inside the code. – Rasim AVCI Jan 26 '18 at 06:40