Friday 15 August 2014

javascript - Promises: q.all() with mixed sync and async functions -


I am still learning about the word and I have to face a problem. Maybe you can help me.

I have a job that does some check before running the actual stuff. Some of these checks are syncs, others are async.

So I have to do something like this:

  q.all ([jenkins.checkConfig, gitlab CheckConfig, sonar.checkConfig]) .then (function () { DoSomethingReallyCoolHere ();});  

But what if Sonar.Check Confief is not a promise? How can I give it futures behavior?

I am currently doing this

  var checkConfig = function () {var qChecked = q.defer (); Var isOK = Maximum SyncTestHere (); If (iioK) {qChecked.resolve (); } And {qChecked.reject (); } Return qChecked.promise; }  

But it looks stupid.

I think this is not a good way to do it, right?

Thank you very much

In order to keep non-wired values ​​in the input array The problem is that they will behave just like those promises which were completed with them.

However, you have to put the actual values ​​(or pledges) in the array, not the returning work. Just go to

  Q.all ([jenkins.checkConfig ()), gitlab.checkConfig (), sonar.checkconfIF ()]). Then (doSomethingReallyCoolHere);  

If you want to throw off your check (and prevent then from callback), then you will actually need to return a disapproved promise that you No deferred should be used, however:

  function check config () {if (terrible sync test heer) back q (true); And return Q.reject (new error ("terrible fail"); }  

Or, if you have a test that actually use throw s, just use

  function checkConfig () {Return Q .try (awesomeSyncTestHere); }  

No comments:

Post a Comment