0

I have method which does some stuff and invokes a callback and after certain time it does the same.

var myFunction = function(callback){
  setInterval(function(){
    var stuff; 
    //do some stuff
    callback(stuff)
  }, 3000);
}

Can I change this to a promise function? If yes how will the user invoke the function? I am planning to use bluebird library.

Neo
  • 4,550
  • 6
  • 34
  • 51
  • 1
    1. You return a promise from `myFunction` 2. you resolve a promise in the `setInterval` – zerkms May 04 '15 at 01:52
  • Can you give a little more detail around what you want to accomplish with the promise? – S McCrohan May 04 '15 at 01:59
  • My understanding is that promise is done when we call resolve on it, can we call resolve multiple times? – Neo May 04 '15 at 02:03
  • @SMcCrohan I am just trying out stuff here... so wanted to know how do stuff correctly in promises. – Neo May 04 '15 at 02:05
  • Okay. A promise doesn't look like it makes sense here on the face of it, because you're not doing anything with the return of your callback, nor waiting on its completion. An example where it WOULD make sense is if you wanted your timer to run 3 seconds from the COMPLETION of `callback` (rather than 3 seconds from the last time the timer triggered) – S McCrohan May 04 '15 at 02:10
  • @SMcCrohan suppose I am at certain interval fetching some data from server. Once the data is back i need to invoke the callback. So should I stick to callback or is there any way I can use promises? – Neo May 04 '15 at 02:20

2 Answers2

6

Promises are one-shot devices. They resolve once and only once and after that their state cannot be changed. As such, they are not good or even viable replacements for recurring events.

To use a promise with something that occurs more than once, you'd have to have some sort of interface that allows you to create a new promise each time and for the caller to somehow have access to that new promise each time. That does not appear to fit with your specific interface at all.

A plain callback like you're using is the most logical interface for a recurring event.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • Oh okay. Since promises does not work here, which one will be better solution pub-sub or callbacks? – Neo May 04 '15 at 02:22
  • 1
    @neolivz4ever - if you have more than one piece of code or even an indeterminate number of pieces of code interested in this event, then you probably want some sort of pub-sub solution to handle all that are interested in the event stream. But, if there's just one piece of code, then a callback is likely simpler and works just fine. – jfriend00 May 04 '15 at 02:24
  • @neolivz4ever check out observables and libraries like RxJS or BaconJS instead. – Benjamin Gruenbaum May 04 '15 at 06:51
0

OP asked in a comment about using a promise to handle the results of an async call made from within the interval-triggered function. That would look like this:

var myFunction = function(callback){
  setInterval(function(){
    var promise = new Promise(function(resolve,reject) {
      var stuff;
      // Do some potentially-long-running work that modifies 'stuff'
      if (stuffWorked)
        resolve(stuff);
      else
        reject(stuff);
    });
    promise.then(function(returnVal) {
      callback(returnVal);
    });
  }, 3000);
}

This is using the ECMAscript 6 form of promises. Lots of other implementations exist...jQuery, q, etc. I can add examples of other flavors if desired.

S McCrohan
  • 6,663
  • 1
  • 30
  • 39
  • Even though we are using promise here internally, who ever using the method will be still calling it with a callback, right? – Neo May 04 '15 at 02:42