Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper way to skip a then function in Q Promises

In my code, based on a specific condition, I would like to skip to the done function, irrespective of all the then functions.

The original version of this question is in the edits. The following is the actual problem I am dealing with. Sorry for the inconvenience

Actual Problem :

I am reading a file and processing it. If the contents of the file match certain conditions, I have to do a series of operations on the file system (say read and write few files) and then to execute done function. If the conditions fail, I have to skip all the series of operations and I have to execute the done function directly.

I return an object (lets say result) in all the then functions and in the next then I update result and return it. So, when all the then are done, done will have the accumulated result. Finally, the done will process result and print it.

So, if the conditions are not met initially, done will simply print result (which would be empty).

Q() .then(readFile) .then(function (contents) {     var processResult = process the contents;     if (processResult) {         return {};     } else {         // How can I skip to `done` from here     } }) .then(function (results) {     // do some more processing and update results     return results; }) ...   // Few more then functions similar to the one above ... .fail(function (exception) {     console.error(exception.stack); }) .done(function (results) {    // do some more processing and update results    console.log(results); }); 
like image 848
thefourtheye Avatar asked Feb 05 '14 12:02

thefourtheye


People also ask

Does promise run without then?

The code inside the Promise constructor runs when the promise is created and it runs synchronously which surprises some people. So even without then() everything still runs.

How do you resolve a promise then?

Promise resolve() method: If the value is a promise then promise is returned. If the value has a “then” attached to the promise, then the returned promise will follow that “then” to till the final state. The promise fulfilled with its value will be returned.

What is then method in promise?

The then method returns a Promise which allows for method chaining. If the function passed as handler to then returns a Promise , an equivalent Promise will be exposed to the subsequent then in the method chain.

How do you make a function wait for promise?

The keyword await is used to wait for a Promise. It can only be used inside an async function. This keyword makes JavaScript wait until that promise settles and returns its result.


1 Answers

It depends a bit on what the conditions to skip are, what kind of operations you are doing, and how “useful” the whole thing is when the conditions failed. You might be able to use a smart rejection here to bring the message across. Otherwise, I believe the correct way to deal with this is really a nested set of promise calls.

This also matches the core idea behind promises, which is to bring back synchronous control structures to asynchronous code execution. In general, when using promises, you should first think about how you would do the task with synchronous code. And if you think about your situation, it would probably work like this:

var contents = readFromFile(); var results = initialOperation(contents); if (fancyCondition(results)) {      results = doSomething(results);      results = doMore(results); } processAndPrint(results); 

So you would have a real branch in there in synchronous code. As such, it makes no sense that you would want to avoid that in asynchronous code using promises. If you could just skip things, you were essentially using jumps with gotos. But instead, you branch off and do some other things separately.

So going back to promises and asynchronous code, having an actual branch with another set of chained operations is completely fine, and actual in spirit of the intent behind promises. So above code could look like this:

readFromFile(fileName) .then(initialOperation) .then(function (results) {     if (fancyCondition(results) {         return doSomething(results)             .then(doMore);     }     return results; }) .catch(errorHandler) .then(processResults) .then(outputResults); // Or `done` in Q 

Also note, that the promise pipeline automatically looks a lot more cleaner when you start using functions that return promises on their own, instead of creating them inline from then.

like image 81
poke Avatar answered Sep 20 '22 06:09

poke