Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Promise.tap #116

Closed
SohumB opened this issue Feb 18, 2014 · 9 comments · Fixed by Woodpile37/bluebird#8 · May be fixed by jalvarez2020/bluebird#1, Qdigital/bluebird#4 or joswa/bluebird#5
Closed

Promise.tap #116

SohumB opened this issue Feb 18, 2014 · 9 comments · Fixed by Woodpile37/bluebird#8 · May be fixed by jalvarez2020/bluebird#1, Qdigital/bluebird#4 or joswa/bluebird#5

Comments

@SohumB
Copy link

SohumB commented Feb 18, 2014

It would be nice for quick debugging for a .tap function to exist, such that

.then(function(obj) { tapFn(obj); return obj })

could be replaced by

.tap(tapFn)

Thanks!

@benjamingr
Copy link
Collaborator

To be fair I'm not sure why we have .call and .get in the library itself. I tend to think of all these things as being 'extras'. I do see the debugging value of this (and debugging seems like pretty much the only time you'd want this since this essentially is causing a side effect).

It is of course very possible to do this from your side just like you described:

Promise.prototype.tap = function(fn){
    return this.then(function(value){
         fn(obj);
         return obj;
    });
};

@SohumB
Copy link
Author

SohumB commented Feb 18, 2014

Fair enough. Thanks!

@SohumB SohumB closed this as completed Feb 18, 2014
@spion
Copy link
Collaborator

spion commented Feb 25, 2014

I like this function...

Here is one use case:

cache.get('foo')
.then(foo => foo ? foo : db.get('foo').tap(cache.put))
.then(res.json);

vs

cache.get('foo')
.then(foo => foo ? foo : db.get('foo').then(foo => cache.put(foo).thenReturn(foo)))
.then(res.json);

Can you come up with other cases? :)

@petkaantonov
Copy link
Owner

Passing cache.put as a function reference is not the same as calling it as a method cache.put() as you know @spion :P

@tgriesser
Copy link
Contributor

FWIW I use tap all the time and would find it a useful core add.

login: function(body) {
  return new User({email: body.email})
    .fetch({require: true})
    .tap(function(user) {
      return bcrypt.compareAsync(body.password, user.password);
    })
    .then(function(user) {
      return user.id
    });
}

@petkaantonov
Copy link
Owner

@tgriesser why are you calling compare but not doing anything with the result? :P

@tgriesser
Copy link
Contributor

Because I don't need to do anything with it, it's purely called to ensure it doesn't throw (if the password is wrong).

@gaearon
Copy link

gaearon commented Mar 7, 2014

Yup, just today I wrote this:

  return this.lastPromise
    .bind(this)
    .then(function (result) {
      if (!this.hasOpenWidget()) {
        this.destroyWidget();
      }

      return result;
    })
    .catch(Promise.CancellationError, $.noop)
    .then(function (result) {
      return result || options.widget.DEFAULT_RESULT;
    });

Note second then returns result because it merely needs to do something else and pass it on.

@petkaantonov petkaantonov reopened this Mar 7, 2014
@petkaantonov
Copy link
Owner

This is in 1.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment