Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I make a synchronous promise in the JavaScript Q library?

Tags:

javascript

q

I want to do something like the following:

delay( 2500 )
  .then( function () { console.log( "Step 1 done" ) } )
  .then( delay( 7500 ) )
  .then( function () { console.log( "Step 2 done" ) } );

So implementation of delay has been demonstrated many times before:

function delay( ms ) {
  var deferred = Q.defer();
  setTimeout( deferred.resolve, ms );
  return deferred.promise;
}

But if I run the above in node.js I get:

... delay of 2500ms
Step 1 done
Step 2 done
... delay of ~7500ms

rather than what I expect to see:

... delay of 2500ms
Step 1 done
... delay of 7500ms
Step 2 done

In the examples provided on https://github.com/kriskowal/q/wiki/Examples-Gallery I can't find any examples of synchronous functions (functions that return a value without any callbacks involved) chained with promise functions.

Any ideas how to mix in synchronous actions with asynchronous promises?

I've tried:

function synchronousPromise() {
  var deferred = Q.defer();
  console.log( "Synchronous function call" );
  deferred.resolve();
  return deferred.promise;
}

delay( 2500 )
  .then( function(){synchronousPromise()} )
  .then( function(){delay( 7500 )} )
  .then( function(){synchronousPromise()} );

And this outputs:

... delay of 2500ms
Time now is 2013-06-20
Time now is 2013-06-20
... delay of 7500ms

.. still not what I'm trying to achieve.

like image 633
PP. Avatar asked Jun 20 '13 12:06

PP.


People also ask

Is Promise in JavaScript synchronous?

A promise is used to handle the asynchronous result of an operation. JavaScript is designed to not wait for an asynchronous block of code to completely execute before other synchronous parts of the code can run. With Promises, we can defer the execution of a code block until an async request is completed.

What is Q defer in node JS?

The q. defer() is used to create deferred which is used to work with promises. The promise will tell the caller that the method is returning some data in some time (async). The caller can then declare logic on the promise then() to be executed when the data is returned.

Is Promise resolve asynchronous?

An async function will return a different reference, whereas Promise.resolve returns the same reference if the given value is a promise. It can be a problem when you want to check the equality of a promise and a return value of an async function.


1 Answers

If you want to chain the callbacks, you have to return a new promise object from one of the callbacks. In your first example, you write

.then( delay( 7500 ) )

which means you are passing a promise object to .then, not a function. According to the Promise/A+ proposal (which Q follows), all non-function arguments must be ignored. So, basically it's the same as if you just write:

delay( 2500 )
  .then( function () { console.log( "Step 1 done" ) } )
  .then( function () { console.log( "Step 2 done" ) } );

Instead, pass function which calls delay and returns the promise object:

delay( 2500 )
  .then( function () { console.log( "Step 1 done" ); } ) 
  .then( function () { return delay( 7500 ); } )
  .then( function () { console.log( "Step 2 done" ); } );

Now the last callback will only be called once the promise object returned by delay in the second callback is resolved.

like image 121
Felix Kling Avatar answered Sep 20 '22 07:09

Felix Kling