Are there differences between .then(functionReference) and .then(function(value){return functionReference(value)})?

前端 未结 4 1407
深忆病人
深忆病人 2020-12-01 18:35

Given a named function utilized to handle a Promise value

function handlePromise(data) {
  // do stuff with `data`
  return data
}
4条回答
  •  醉梦人生
    2020-12-01 19:07

    There is indeed one main difference though not so important, which actually should be avoided. If you call handlePromise() as directly inside then() you're not allowed to add external parameters. If you call it inside function which is inside then .then( (result) => handle(result, someOtherParam) ) then you're able to add 'someotherparam' to call. It's handy if you have bigger calls tree, though I would avoid that and pass it through whole promises chain.

    TLDR; first one is called with .then() results as parameters, latter one is elastic in what you want to pass further.

提交回复
热议问题