Ren
Ren

Reputation: 1051

Order of async functions execution

I am dealing with an old codebase and faced this situation where it's difficult for me to understand the order of execution after promises are resolved. I am more familiar with async/await syntax or with a chain of then-s, but not with this one. Here is the snippet:

_loadCaseDetail: funciton (arg1, arg2, arg3) {
  var oDataModel = this.getOwnerComponent().getModel('db2');

  loadLatestDatasetVersion(oDataModel).then(function (datasetVersion) {
    // do something
  });

  loadCountries(oDataModel).then(function (countries) {
    // do something
  });

  numberOfRulesetChanges(oDataModel).then(function (elements) {
    // do something
  });

  return fireImplicitLock(caseUuid).then(function (lockData) {
    // do something
  }).then(function () {
    // do something
  })
}

loadLatestDatasetVersion, loadCountries, numberOfRulesetChanges, fireImplicitLock - all return promises

My question is: What would be the order in this case for all then-s that come after these promises?

Is it exactly sequential as it is or it's not and we can refactor it with say Promise.all?

Does it even need any refactoring?

Upvotes: 3

Views: 131

Answers (2)

Akshay Bande
Akshay Bande

Reputation: 2587

loadLatestDatasetVersion, loadCountries, numberOfRulesetChanges, fireImplicitLock - all return promises they will get inserted into Event loop one after another.

then part of promises will get executed once promises are resolved. Which promise's then will get executed depends upon the execution of the respective promise.

_loadCaseDetail: funciton (arg1, arg2, arg3) {
  var oDataModel = this.getOwnerComponent().getModel('db2');

  loadLatestDatasetVersion(oDataModel).then(function (datasetVersion) {
    // do something
  });

  loadCountries(oDataModel).then(function (countries) {
    // do something
  });

  numberOfRulesetChanges(oDataModel).then(function (elements) {
    // do something
  });

  return fireImplicitLock(caseUuid).then(function (lockData) {
    // do something
  }).then(function () {
    // do something
  })
}

Promise.all is like a gate where you can pass array of promises and it will resolve only after all the promises are resolved.

let p1 = Promise.resolve(loadLatestDatasetVersion(oDataModel));

let p2 = Promise.resolve(loadCountries(oDataModel));

let p3 = Promise.resolve(numberOfRulesetChanges(oDataModel));

let p4 = Promise.resolve( fireImplicitLock(caseUuid)

let finalPromise = Promise.all([p1,p2,p3,p4]);

finalPromise.then(([datasetVersion,countries,elements,lockData])=>{

    // do something here with datasetVersion, countries, elements, lockData you have all the params cause all the promises are resolved.

})

Same thing you can achieve using Promise.all like shown above.

More about promises

return fireImplicitLock(caseUuid).then(function (lockData) {
    // do something
  }).then(function () {
    // do something
  })

Above line will not return any result from promises, it will return Promise object with its status resolved/pending/rejected and value.

Upvotes: 1

Quentin
Quentin

Reputation: 943210

What would be the order in this case for all then-s that come after these promises?

The then function will fire when the associated promise resolves. That's the point of asynchronous code. It goes away until it is ready to do the next thing.

we can refactor it with say Promise.all

You could use Promise.all to wait until multiple promises are resolved before doing anything with the resulting values, but that would be inefficient unless the then function for C requires data from A or B.

Upvotes: 2

Related Questions